如何使用 MUI Texfield 和 Redux 更改状态

Sam*_*nce 6 javascript reactjs redux material-ui

我有一个输入字段,在进入单独的页面之前我试图在其中传递一些信息。我的问题是 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;
Run Code Online (Sandbox Code Playgroud)

虽然我的 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>
  );
}
Run Code Online (Sandbox Code Playgroud)

我的商店看起来很标准:

export default configureStore({
  reducer: {
    wallet: walletReducer,
  },
});
Run Code Online (Sandbox Code Playgroud)

小智 3

我假设您需要在减速器函数中使用正确的状态字段名称。我猜下面的代码行会产生问题,

state.value = action.payload;
Run Code Online (Sandbox Code Playgroud)

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

state.wallet = action.payload;
Run Code Online (Sandbox Code Playgroud)