【发布时间】:2019-05-27 13:55:32
【问题描述】:
我正在调用一个 handleClick 函数,该函数调用 fetchApi 操作,它将响应存储在 redux 中并返回一个承诺,我已将一个 .then 附加到我应该从 redux 存储中获取更新值的地方,我得到了外部 .then 重新渲染,但不在内部 .then 你可以查看这个代码沙箱https://codesandbox.io/s/what-is-the-reason-z6qt6
function App(props) {
console.log(props.fetchApiRes, "outside handleclick");
const getProps = () => props;
function handleClick() {
props.fetchApi().then(() => {
console.log(props.fetchApiRes, "inside then");
console.log(getProps().fetchApiRes, "still not getting ....");
});
}
return (
<div className="App">
<h1>Hello CodeSandbox</h1>
<h2>Start editing to see some magic happen!</h2>
<button onClick={handleClick}>click</button>
</div>
);
}
const mapDisptach = {
fetchApi
};
const mapProps = ({ fetchReducer }) => {
return {
fetchApiRes: fetchReducer.fetchApi
};
};
【问题讨论】:
-
我相信这是有意的行为。如果更新了跟踪的道具,则应该重新渲染,这是您应该使用道具的地方。函数调用中的 props 对象仍然是陈旧的 props 对象。
标签: javascript reactjs