【问题标题】:How to change state with MUI Texfield & Redux如何使用 MUI Texfield 和 Redux 更改状态
【发布时间】:2022-06-11 14:02:21
【问题描述】:

我有一个输入字段,我尝试在其中传递一些信息,然后再转到单独的页面。我的问题是 Redux 状态没有改变,但控制台显示值正在正确传递。我假设我的 Slice 有问题,但我相信我正确传递了有效载荷。我的 Redux 切片看起来像:

import { createSlice } from "@reduxjs/toolkit";

export const walletSlice = createSlice({
  name: "wallet",
  initialState: {
    wallet: "xxx-xxxx-xxx-xxxx",
  },
  reducers: {
    setWalletAddress: (state, action) => {
      state.value = action.payload;
    },
  },
});

export const { setWalletAddress } = walletSlice.actions;

export default walletSlice.reducer;

虽然我的 from 组件看起来像:

import { setWalletAddress } from "../../redux/wallet";
import { useDispatch } from "react-redux";

export default function AddressForm() {
return (
  const dispatch = useDispatch();
  const handleChangeWallet = (event) => {
    dispatch(setWalletAddress (event.target.value));
    console.log(event.target.value);
  };
    <React.Fragment>
          <TextField
            onChange={handleChangeWallet}
            label="Wallet address"
          />
    </React.Fragment>
  );
}

我的商店看起来很标准:

export default configureStore({
  reducer: {
    wallet: walletReducer,
  },
});

【问题讨论】:

    标签: javascript reactjs redux material-ui


    【解决方案1】:

    我假设您需要使用正确的状态字段名称。我猜下面的代码行会引起问题,

    state.value = action.payload;
    

    您需要写正确的字段名称wallet 而不是value

    state.wallet = action.payload;
    

    【讨论】:

      【解决方案2】:

      在您的调度中,您使用的是setEmail 而不是setWalletAddress

      【讨论】:

      • 我使用了错误的变量,我的代码使用了 setWalletAddress。谢谢
      猜你喜欢
      • 2019-08-09
      • 2018-05-19
      • 2018-05-21
      • 1970-01-01
      • 2020-04-26
      • 2016-08-31
      • 2021-07-02
      • 2018-12-08
      • 2021-10-03
      相关资源
      最近更新 更多