易用useStoreActions是否不立即更新状态?

时间:2020-06-14 03:59:49

标签: javascript reactjs react-native redux easy-peasy

让我们说这是我的代码

if(delta >= NS_PER_UPDATE){
    missedTime = delta - NS_PER_UPDATE;
    old = System.nanoTime() - missedTime;
    render();
    frames++;
}

当我尝试console.log时,它不显示新的捐赠数据

1 个答案:

答案 0 :(得分:0)

在easy-peasy中,initialState是用于初始化商店的不可变值。因此,您的setDonation函数将无法更改此值。

此处显示了您想做的完整(尽管做作!)示例,并附有注释,可以解释发生了什么情况:

import React, { Component } from "react";
import { render } from "react-dom";

import {
  useStoreState,
  action,
  createStore,
  StoreProvider,
  useStoreActions
} from "easy-peasy";

// Define your model
const donationModel = {
  donation: {},
  setDonation: action((state, payload) => {
    state.donation = payload;
  })
};

// Define you application store
const storeModel = {
  donations: donationModel
};

// Create an instance of the store
const store = createStore(storeModel);

const App = () => (
  // Wrap the Donation component with the StoreProvider so that it can access the store
  <StoreProvider store={store}>
    <Donation />
  </StoreProvider>
);

const Donation = () => {
  // Dispatch a setDonation action to add donation data to the store
  useStoreActions(actions =>
    actions.donations.setDonation({
      amount: 1000000,
      message: "donation from easy peasy",
      payment_method_id: "1",
      receiver_id: "1"
    })
  );

  // Retrieve data from the store using useStoreState
  const donationMessage = useStoreState(
    state => state.donations.donation.message
  );

  // Display the donation message returned from the store!
  return <>{donationMessage}</>;
};

render(<App />, document.getElementById("root"));

您可以找到有效的here

相关问题