gpt4 book ai didi

reactjs - useSelector 常量在发送后不更新

转载 作者:行者123 更新时间:2023-12-05 02:09:52 26 4
gpt4 key购买 nike

这是一个Codesandbox .

getIDs() 更新 cells,然后 initializeCells() 需要它。但是,此更改不会在调度操作后反射(reflect)出来。尽管如此,我还是可以在 Redux dev tools 上看到该操作已完成并且 cells 的值已相应更改。 gameStart() 通过 props 传递给子组件 cells,并通过 useEffect() Hook 调用。我需要传递一个空数组作为此 Hook 的第二个参数,否则它将永远运行,因为每次调用时状态都会更新。问题是在首次运行 getIDs() 之后,新状态不适用于以下函数。似乎是在 gameStart() 完全完成并再次调用时。我需要在 getIDs() 完成后立即更新 initializeCells() 需要的状态。

细胞.js

import React, { useEffect } from "react";
import { useSelector } from "react-redux";

import Cell from "./Container/Container/Cell";

const Cells = props => {
const board = useSelector(state => state.board);

useEffect(() => {
props.gameStart();
}, []);

return (
<div id="cells">
{board.map(cell => {
return (
<Cell
id={cell.id.substring(1)}
key={cell.id.substring(1)}
className="cell"
/>
);
})}
</div>
);
};

export default Cells;

app.js

import React, { useEffect } from "react";
import { useSelector, useDispatch } from "react-redux";

import {
setCells,
setBoard
} from "../../redux/actions/index";

const Game = () => {
const dispatch = useDispatch();

const cells = useSelector(state => state.cells);
const board = useSelector(state => state.board);
const boardSize = useSelector(state => state.boardSize);

async function gameStart() {
await getIDs();
console.log(cells); // []
await initializeCells();
await assignSnake();
await placeFood();
await paintCells();
}

function getIDs() {
let cellID = "";
let collection = [];

for (let i = 1; i <= boardSize.rows; i++) {
for (let j = 1; j <= boardSize.columns; j++) {
cellID = `#cell-${i}-${j}`;

collection.push(cellID);
}
}
dispatch(setCells(collection));
console.log(cells); // []
}

function initializeCells() {
console.log(cells); // []
const board = [];
// for loop never runs because cells is empty
for (let i = 0; i < cells.length; i++) {
board.push(cell(cells[i]));
}
dispatch(setBoard(board));
console.log("Board: ", board); // []
}

function cell(id) {
return {
id: id,
row: id.match("-(.*)-")[1],
column: id.substr(id.lastIndexOf("-") + 1),
hasFood: false,
hasSnake: false
};
}

return (
...
)
}

export default Game;

reducers/index.js

import {
SET_CELLS,
SET_BOARD
} from "../constants/action-types";

const initialState = {
board: [],
cells: [],
boardSize: {
rows: 25,
columns: 40
}
};

const rootReducer = (state = initialState, action) => {
switch (action.type) {
case SET_CELLS:
return Object.assign({}, state, {
cells: action.payload
});

case SET_BOARD:
return Object.assign({}, state, {
board: action.payload
});

default:
return state;
}
};

actions/index.js

import {
SET_CELLS,
SET_BOARD
} from "../constants/action-types";

export const setCells = payload => {
return { type: SET_CELLS, payload };
};

export const setBoard = payload => {
return { type: SET_BOARD, payload };
};

constants/action-types.js

export const SET_CELLS = "SET_CELLS";
export const SET_BOARD = "SET_BOARD";

最佳答案

在调度一些 Action 后,更新的商店将仅在下一次渲染时提供。这对于带有钩子(Hook)的函数和带有 connect HOC 的类都是一样的。

您需要更改您的代码,而不是期望立即更改。我很难理解你在这里的意图,你可以从渲染它来的东西开始,然后用行动分派(dispatch)和忘记的方法。它应该有效。

如果没有,制作最小样本(只有相关的钩子(Hook)+数据是如何呈现的)并描述你想要得到什么(而不是“如何”)

关于reactjs - useSelector 常量在发送后不更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59489092/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com