我是 ReactJS
的新手我有三个组件 App.js
, Login.js
, Register.js
当 wepapp 启动时,它首先启动 App.js
在该组件中,我有两个按钮“登录”和“注册”,通过单击“登录”按钮,它应该转到 LogIn
组件并单击“注册”按钮,它应该转到 Register
组件,我没有找到一种干净的方法来实现这一目标。
这是我的代码 App.js
import React, { Component } from 'react';
import logo from './logo.svg';
import './App.css';
class App extends Component {
constructor(props) {
super(props);
// This binding is necessary to make `this` work in the callback
this.gotoLogin = this.gotoLogin.bind(this);
this.gotoRegister = this.gotoRegister.bind(this);
}
gotoLogin() {
alert("Login");
}
gotoRegister() {
alert("Register");
}
render() {
return (
<div className="App">
<div className="App-header">
<img src={logo} className="App-logo" alt="logo" />
<h2>Welcome to React</h2>
</div>
<button onClick={this.gotoLogin} class="Button ">Login</button>
<button onClick ={this.gotoRegister} class="Button">Register</button>
</div>
);
}
}
export default App;
代码index.js
import React from 'react';
import ReactDOM from 'react-dom';
import App from './App';
import './index.css';
ReactDOM.render(
<App />,
document.getElementById('root')
);
分别我还有 Login.js and Register.js
的组件类
最佳答案
render() {
return (
<div className="App">
<div className="App-header">
<img src={logo} className="App-logo" alt="logo" />
<h2>Welcome to React</h2>
</div>
<button href='#/login' onClick={this.gotoLogin} class="Button ">Login</button>
<button href='#/register' onClick ={this.gotoRegister} class="Button">Register</button>
</div>
);
}
关于javascript - 想要将一个组件路由到另一组件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43136721/