【发布时间】:2020-05-01 19:52:20
【问题描述】:
我想知道为什么函数在调度时被多次调用。 另外,有没有更好的办法呢,
getData = (value) =>{
const body = {
value: value,
cn: "TH"
}
return body;
}
renderData(){
console.log("starts");
this.props.dispatch(queryData(this.getData(10)));// called repeatedly
}
render(){
return(
<div>{this.renderData()}</div>
)
}
我已经更新了相同类型的不同场景,我想知道有没有其他更好的方法。
场景 2
getData = () =>{
const body = {
value: value,
cn: "TH"
}
return body;
}
componentDidMount=()=>{
this.props.dispatch(queryData(this.getData()))
}
componentDidUpdate = () => {
const {allstate} = this.props.querydata
if(this.props.querydata){
this.renderState(allstate);
}
}
renderState = (data) => {
const getId = data.map(e=>e.id); //will get array of values [2, 3, 4]
getid.map(e=>
this.props.dispatch(queryState(e))); //for every id dispatch props,
)
}
render(){
// will gets this by componentDidMount call
return (
<div>
</div>
)
}
【问题讨论】:
-
您不应该在渲染方法中修改状态,因为正是这个原因。
标签: javascript reactjs redux react-redux redux-saga