在分派重击动作时,状态会更新,但调用分派的函数中的相关状态仍旧

时间:2020-08-21 03:55:54

标签: javascript reactjs react-redux redux-thunk

以下分派应该失败。但是redux存储将被更新。 handleBtnClick内部的日志记录不会在范围中显示更新的值。

const App = () => {
  const dispatch = useDispatch();
  const storeData = useSelector((state) => state.some);

  const { error } = storeData;

  console.warn("Outside", error);

  const handleBtnClick = async () => {
    await dispatch(getWeather());
    // This will display the old stat
    console.warn("inside", error);
  };

  const handleSubmit = (values) => console.warn(values);

  return (
    <div className="App">
      {console.log("in Render", error)}
      <h1>Hello</h1>
      <h2>Start editing to see some magic happen!</h2>

      <button onClick={handleBtnClick}>Do Api Call</button>
    </div>
  );
};

Link to Sandbox

0 个答案:

没有答案