Javascript 在react中设置状态后不修改状态值
我知道setState是一个异步操作,所以我进行了一个同步调用,以确保在我的Javascript 在react中设置状态后不修改状态值,javascript,reactjs,Javascript,Reactjs,我知道setState是一个异步操作,所以我进行了一个同步调用,以确保在我的组件将接收props触发器之前更新状态值,但它仍然显示以前的值。此代码已在my child元素中编写。我怀疑在我的父元素中有什么事情要做吗?或者有其他原因吗?下面是我的代码 constructor(props) { super(props); this.state = { loading: false, yearValue:Number(localStorage.getItem(
组件将接收props
触发器之前更新状态值,但它仍然显示以前的值。此代码已在my child元素中编写。我怀疑在我的父元素中有什么事情要做吗?或者有其他原因吗?下面是我的代码
constructor(props) {
super(props);
this.state = {
loading: false,
yearValue:Number(localStorage.getItem('year'))//this is 2019
};
}
componentDidMount() {
}
componentWillReceiveProps(nextProps) {
console.log(
'before if condition...',
nextProps,
Number(localStorage.getItem('year'))
);
if (
this.state.yearValue ===
Number(localStorage.getItem('year')) //this condition satisfied twice before it changed the value
) {
console.log('inside if condition.....', nextProps);
this.setSstate = {
loading: false
};
} else this.setState({ loading: false });
}
updatePrograValues = year => {//year = 2017
if (year === Number(localStorage.getItem('year'))) {
this.props.showPlanYearValues(year);
} else {
this.setState({
loading: true
},this.props.tooltipRefresher());
}
};
如果我承担了错误的
在您的setState
调用中,code>是问题的输入错误,而不是实际代码中的输入错误,问题在于:
this.setState({
loading: true
},this.props.tooltipRefresher());
调用this.props.tooltipRefresher()
,然后将其返回值传递到setState
,与foo(bar())
调用bar
并将其返回值传递到foo
的方式完全相同
如果您希望在状态值更新后调用它,请从中删除()
,这样您就不会调用它,而是将它传递到setState
中,这样setState
将在设置状态时调用它:
this.setState({
loading: true
},this.props.tooltipRefresher);
// No () --------------------^
如果它对此
的值敏感(例如,工具提示刷新器
期望此
为此。道具
),您可能需要绑定
:
this.setState({
loading: true
},this.props.tooltipRefresher.bind(this.props));
…或箭头函数:
this.setState({
loading: true
},() => this.props.tooltipRefresher());
如果我承担了错误的
在您的setState
调用中,code>是问题的输入错误,而不是实际代码中的输入错误,问题在于:
this.setState({
loading: true
},this.props.tooltipRefresher());
调用this.props.tooltipRefresher()
,然后将其返回值传递到setState
,与foo(bar())
调用bar
并将其返回值传递到foo
的方式完全相同
如果您希望在状态值更新后调用它,请从中删除()
,这样您就不会调用它,而是将它传递到setState
中,这样setState
将在设置状态时调用它:
this.setState({
loading: true
},this.props.tooltipRefresher);
// No () --------------------^
如果它对此
的值敏感(例如,工具提示刷新器
期望此
为此。道具
),您可能需要绑定
:
this.setState({
loading: true
},this.props.tooltipRefresher.bind(this.props));
…或箭头函数:
this.setState({
loading: true
},() => this.props.tooltipRefresher());
对不起,你能再详细说明一下吗。您的意思是说我从
tooltipRefresher
返回的任何内容都将返回到状态?您需要将一个函数传递给setState,该函数将在状态更新和组件重新提交后调用。目前,您正在传递函数ToolTipRefresh返回的值,因为您实际上正在调用它。如果删除括号,您将传递实际的函数,您的问题将得到解决。哦..我需要认真考虑何时使用bind,何时不使用toWell,binding所做的一切就是将函数中的设置为传递给bind
函数的值。如果您确实需要将this
设置为ToolTipRefresh中的当前组件,则需要执行this.props.ToolTipRefresh.bind(this)
,而不是this.params
。我认为为OPs问题提供箭头函数解决方案是个好主意。在React社区中,回调要简单得多,而且非常普遍<代码>()=>这个.props.tooltiprefresh()
很抱歉,请您详细说明一下。您的意思是说我从tooltipRefresher
返回的任何内容都将返回到状态?您需要将一个函数传递给setState,该函数将在状态更新和组件重新提交后调用。目前,您正在传递函数ToolTipRefresh返回的值,因为您实际上正在调用它。如果删除括号,您将传递实际的函数,您的问题将得到解决。哦..我需要认真考虑何时使用bind,何时不使用toWell,binding所做的一切就是将函数中的设置为传递给bind
函数的值。如果您确实需要将this
设置为ToolTipRefresh中的当前组件,则需要执行this.props.ToolTipRefresh.bind(this)
,而不是this.params
。我认为为OPs问题提供箭头函数解决方案是个好主意。在React社区中,回调要简单得多,而且非常普遍<代码>()=>此.props.ToolTipRefresh()工具