gpt4 book ai didi

javascript - 我如何在 react 中向按钮添加条件渲染

转载 作者:行者123 更新时间:2023-11-29 17:46:52 26 4
gpt4 key购买 nike

我有点新 react 我只是想知道如何在我的登录页面中添加条件渲染到按钮,只要用户名和密码与 api 用户名和密码匹配,点击提交按钮后它应该给我新的组件。

//here is api
const api ='http://localhost:3000/api/login';
//Login Component
export class Login extends Component {
state = {
username: '',
password: '',
confirmuser:'',
confirmpass:'',
};
componentDidMount() {
axios.get(api).then(response => {
let number = response.data.username;
console.log(response);
let number2 = response.data.password;
this.setState({ confirmuser:number});
this.setState({ confirmpass: number2 });
})
.catch((err)=>{
console.log('Looks like something went wroong ',err);
});
}
//HandleClick Method
handleClick=()=>{
const username = this.state.username;
const password= this.state.password;
const confirmuser=this.state.confirmuser;
const confirmpass=this.state.confirmpass;
console.log(confirmuser);
console.log(username);
console.log(password);
console.log(confirmpass);
if (username == confirmuser && password == confirmpass ){
console.log(`username is ${username}`);
}
else{
console.log('Please enter right username or password');
}
};

//Rendaring the elements from material-ui
render() {
return (
<div>
<div>
<AppBar
title="Login"
/>
<TextField
hintText="Enter your Username"
floatingLabelText="Username"
onChange={(event, newValue) => this.setState({ username: newValue })}
/>
<br />
<TextField
type="password"
hintText="Enter your Password"
floatingLabelText="Password"
onChange={(event, newValue) => this.setState({ password: newValue })}
/>
<br />
<RaisedButton label="Submit" primary={true} style={style} onClick={ (event) => this.handleClick(event)} />
</div>
</div>
);
}
};

点击按钮提交后如何渲染

最佳答案

如果你想重定向到另一个组件,有两种方法。然后使用 react-router 4 来这样做。使用 this .

     handleClick=()=>{
const username = this.state.username;
const password= this.state.password;
const confirmuser=this.state.confirmuser;
const confirmpass=this.state.confirmpass;
console.log(confirmuser);
console.log(username);
console.log(password);
console.log(confirmpass);
if (username == confirmuser && password == confirmpass ){
this.props.history.push({ pathname: '/yourcomponent',});
}
else{
console.log('Please enter right username or password');
}
};

将您的组件包装在 withRouter HOC 中以使其工作。

else 如果你想在同一页面中呈现组件(更简单的方法)。然后这样做。

       constructor(props){
super(props);
this.state={
loginSuccess:false
}
}


handleClick=()=>{
const username = this.state.username;
const password= this.state.password;
const confirmuser=this.state.confirmuser;
const confirmpass=this.state.confirmpass;
console.log(confirmuser);
console.log(username);
console.log(password);
console.log(confirmpass);
if (username == confirmuser && password == confirmpass ){
this.setState({loginSuccess:true});
}
else{
console.log('Please enter right username or password');

}
};

renderComp(){
if(this.state.loginSuccess===true){
return(<YourComponent/>);
}
else{
return(<div>Incorrect UserName or Password</div>)
}
}

//Rendaring the elements from material-ui
render() {
return (
<div>
<div>
<AppBar
title="Login"
/>
<TextField
hintText="Enter your Username"
floatingLabelText="Username"
onChange={(event, newValue) => this.setState({ username: newValue })}
/>
<br />
<TextField
type="password"
hintText="Enter your Password"
floatingLabelText="Password"
onChange={(event, newValue) => this.setState({ password: newValue })}
/>
<br />
<RaisedButton label="Submit" primary={true} style={style} onClick={ (event) => this.handleClick(event)} />
</div>
//change here
{this.renderComp()}

</div>
);
}
};

关于javascript - 我如何在 react 中向按钮添加条件渲染,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48533780/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com