Javascript 如何在组件中设置状态并在分派操作后立即装载
在我的组件中,我已经在组件挂载之后发送了一个操作。这里的问题是,当调度操作时,它从服务器获取数据,我想将这些数据添加到本地状态。但如果我把Javascript 如何在组件中设置状态并在分派操作后立即装载,javascript,reactjs,Javascript,Reactjs,在我的组件中,我已经在组件挂载之后发送了一个操作。这里的问题是,当调度操作时,它从服务器获取数据,我想将这些数据添加到本地状态。但如果我把setState放在这样的组件中,它就不起作用了。 这里getParams如果有可用值,则从url获取值 componentDidMount() { const getParam = this.getParamsValue(); const year = getParam.year ? getParam.year : 2020; thi
setState
放在这样的组件中,它就不起作用了。
这里getParams
如果有可用值,则从url获取值
componentDidMount() {
const getParam = this.getParamsValue();
const year = getParam.year ? getParam.year : 2020;
this.props.getFilterData(year); // <-- here this is the dispatch functions.
if (this.props.dispensingData.loading) { // <-- this is coming from redux state.
this.setState({
selectTab: getParam.tab ? parseInt(getParam.tab) : 0,
dataTo: getParam.to ? getParam.to : this.props.dispensingData.to,
dataFrom: getParam.from ? getParam.from : this.props.dispensingData.from,
dataType: getParam.type ? getParam.type : this.props.dispensingData.type,
year: year,
timeSpan: getParam.period ? getParam.period : "monthly",
});
}
}
componentDidMount(){
const getParam=this.getParamsValue();
const year=getParam.year?getParam.year:2020;
this.props.getFilterData(年);//使用此
componentWillReceiveProps(nextProps){
const getParam = this.getParamsValue();
const year = getParam.year ? getParam.year : 2020;
if (nextProps.dispensingData.loading) { <-- this is comming from redux state.
this.setState({
selectTab: getParam.tab ? parseInt(getParam.tab) : 0,
dataTo: getParam.to ? getParam.to : nextProps.dispensingData.to,
dataFrom: getParam.from ? getParam.from : nextProps.dispensingData.from,
dataType: getParam.type ? getParam.type : nextProps.dispensingData.type,
year: year,
timeSpan: getParam.period ? getParam.period : "monthly",
});
}
}
组件将接收道具(下一步){
const getParam=this.getParamsValue();
const year=getParam.year?getParam.year:2020;
如果(nextrops.dispensingData.loading){使用这个
componentWillReceiveProps(nextProps){
const getParam = this.getParamsValue();
const year = getParam.year ? getParam.year : 2020;
if (nextProps.dispensingData.loading) { <-- this is comming from redux state.
this.setState({
selectTab: getParam.tab ? parseInt(getParam.tab) : 0,
dataTo: getParam.to ? getParam.to : nextProps.dispensingData.to,
dataFrom: getParam.from ? getParam.from : nextProps.dispensingData.from,
dataType: getParam.type ? getParam.type : nextProps.dispensingData.type,
year: year,
timeSpan: getParam.period ? getParam.period : "monthly",
});
}
}
组件将接收道具(下一步){
const getParam=this.getParamsValue();
const year=getParam.year?getParam.year:2020;
如果(nextrops.dispensingData.loading){使用getDerivedStateFromProps方法,该方法将在props更新时触发。此函数期望返回一个状态。因此,您需要返回该状态而不是setState
static getDerivedStateFromProps(props,state){
if(props.dispensingData.loading){
return {
...state,
selectTab: getParam.tab ? parseInt(getParam.tab) : 0,
dataTo: getParam.to ? getParam.to : nextProps.dispensingData.to,
dataFrom: getParam.from ? getParam.from : nextProps.dispensingData.from,
dataType: getParam.type ? getParam.type : nextProps.dispensingData.type,
year: year,
timeSpan: getParam.period ? getParam.period : "monthly",
}
}
else{
return state;
}
}
但是,这不是推荐的方法。您可以在render method中计算上述值,而不是设置状态。因为当道具更改时,render将被触发。这样可以避免设置不必要的状态,从而再次触发render方法。使用getDerivedStateFromProps方法,该方法将在道具更新时被触发。此函数要求返回一个状态。因此,您需要返回状态,而不是setState
static getDerivedStateFromProps(props,state){
if(props.dispensingData.loading){
return {
...state,
selectTab: getParam.tab ? parseInt(getParam.tab) : 0,
dataTo: getParam.to ? getParam.to : nextProps.dispensingData.to,
dataFrom: getParam.from ? getParam.from : nextProps.dispensingData.from,
dataType: getParam.type ? getParam.type : nextProps.dispensingData.type,
year: year,
timeSpan: getParam.period ? getParam.period : "monthly",
}
}
else{
return state;
}
}
但是,这不是推荐的方法。您可以在render method中计算上述值,而不是设置状态。因为当道具更改时,render将被触发。这样可以避免设置不必要的状态,从而再次触发render方法。使用getDerivedStateFromProps方法,该方法将在道具更新时被触发。T他的函数要求返回一个状态。因此,您需要返回状态而不是setState。使用getDerivedStateFromProps方法,该方法将在props更新时触发。此函数要求返回状态。因此,您需要返回状态而不是setState。