我有一个输入字段,在进入单独的页面之前我试图在其中传递一些信息。我的问题是 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) => { …Run Code Online (Sandbox Code Playgroud) 我有数据试图获取并与下面的代码一起使用,但是当我记录记录的值时,我首先得到一个空数组,然后得到我获取的对象。因此,我无法在我的应用程序中访问该对象的信息。
const [records, setRecords] = useState([]);
useEffect(() => {
async function getRecords() {
const response = await fetch(`http://localhost:5000/record`);
if (!response.ok) {
const message = `An error occurred: ${response.statusText}`;
window.alert(message);
return;
}
const records = await response.json();
setRecords(records);
}
getRecords();
return;
}, []);
console.log(records);
Run Code Online (Sandbox Code Playgroud)