[英]setState function in reactjs is not setting state. How to overcome this problem?
To open a bootstrap modal I'm setting state {isOpen: true} but setState doesn't update the state 要打开引导模式,请设置状态{isOpen:true},但setState不会更新状态
I've used async/await , setTimeout but nothing works.Same modal is opening in my another component. 我已经使用了async / await,setTimeout,但是没有任何效果。同一模态正在我的另一个组件中打开。
import React from 'react';
import Dialog from 'react-bootstrap-dialog'
import { Button } from 'react-bootstrap'
import { Modal } from 'react-bootstrap'
class EventComponent extends React.Component {
constructor(props) {
super(props);
this.state = {
isOpen: false
};
this.onClickdialog = this.onClickdialog.bind(this);
this.toggleModal = this.toggleModal.bind(this);
}
toggleModal = () => {
console.log('open', this.state.isOpen)
this.setState({ isOpen: !this.state.isOpen }, console.log('open',
this.state.isOpen));
}
onClickdialog() {
debugger
this.toggleModal();
// this.dialog.show('Hello Dialog!')
}
renderEvent(event) {
const eventType = event.EventType;
const name = event.Name;
const isApproved = event.IsApproved === 1 ? 'approved' : 'unapproved';
switch (eventType) {
case 'Birthday': return (<div className='birthday' title={eventType}>
<i className="fas fa-birthday-cake" ></i> {name}
</div>);
case 'Full Day': return (<div className={`fullday ${isApproved}`} title=
{eventType}>
<i className="fas fa-umbrella-beach"></i> {name}
<i style={{ marginTop: '-2px', position: 'absolute' }} >
<a onClick={this.onClickdialog.bind(this)} style={{ fontWeight:
'bold' }}>...</a></i>
</div>);
default: return (<div>{eventType}: {name}</div>);
}
}
render() {
return (
<div>
{this.renderEvent(this.props.event)}
<Modal className={"override-fade"} show={this.state.isOpen}
onClose={this.toggleModal}>
</Modal>
</div>
)
}
}
export default EventComponent;
Expecting isOpen state to change on updating state on click 预期isOpen状态会随着点击状态的更新而改变
Just as Yanis said, you´re logging it wrong. 就像亚尼斯(Yanis)所说,您记错了。 Second argument of setState needs to be a callback function, however you´re calling console.log right away. setState的第二个参数必须是一个回调函数,但是您立即调用console.log。 Instead do: 而是:
this.setState({ isOpen: !this.state.isOpen }, () => console.log(...))
btw, you don´t have to bind class properties that is defined using arrow functions 顺便说一句,您不必绑定使用箭头函数定义的类属性
You can use previous state in setState
api. 您可以在setState
api中使用以前的状态。
toggleModal = () => {
this.setState((state) => ({
isOpen: !state.isOpen
}))
}
here, state represents previous state. 在这里,状态代表先前的状态。
and also please can you remove extra bind from the onClick
event. 并且还可以从onClick
事件中删除多余的绑定。
<a onClick={this.onClickdialog} style={{ fontWeight: 'bold' }}>...</a>
I think you might probably check it wrong way. 我认为您可能会以错误的方式检查它。 Does this console.log works for you? 这个console.log对您有用吗?
this.setState({ isOpen: !this.state.isOpen }, console.log('open',
this.state.isOpen))
I think instead you should try something like 我认为相反,您应该尝试类似
this.setState(
{ isOpen: !this.state.isOpen },
() => {
console.log('open', this.state.isOpen))
}
Try This, 尝试这个,
toggleModal = () => {
console.log('open', this.state.isOpen)
this.setState({ isOpen: !this.state.isOpen });
console.log(this.state.isopen);}
check below function, 检查以下功能,
toggleModal = () => {
const { isOpen } = this.state;
console.log('before ', isOpen);
this.setState((prevState) => {
// prevState before updating state
console.log('before changing state', prevState.isOpen);
return {
isOpen: !prevState.isOpen,
};
}, () => {
// callback after setting state
const { isOpen: afterIsOpen } = this.state;
console.log('after update ', afterIsOpen);
});
}
Your code actually works try adding something in your Modal like this: 您的代码实际上可以正常工作,尝试在Modal中添加如下内容:
return (
<div>
{this.renderEvent(this.props.event)}
<Modal className={"override-fade"} show={this.state.isOpen}
onClose={this.toggleModal}>
<h1>test</h1>
</Modal>
</div>
)
and then give it a try. 然后尝试一下。 Also I believe that you didn't import bootsrap's css file you should import it. 我也相信您没有导入bootsrap的css文件,应该将其导入。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.