我在Home
和Result
上有两条路线。 Home
监视其两个状态id
和count
。在Home
内部,有一个嵌套组件Page
,其状态为check
,并呈现一个按钮。单击按钮可触发事件:1)如果count
为true,则递增check
; 2)如果id
<3,则递增id
;否则,将count
传递给另一个路由Result
(props.history.push
)。
为了简化此示例中的问题,我将check
设置为true
。由于id
从0开始,所以当Route更改时,count
应该是3,但我在这里只有2。我试图先同步调用setState
来递增count
,然后再传递给Result
,但props.history.push
会忽略setState
并自行执行。>
处理这种情况或类似情况(不修改组件或状态层次结构)的最佳实践是什么? 请在下面找到示例代码和屏幕截图。
App.js
<BrowserRouter>
<Switch>
<Route path="/home" component={Home} />
<Route path="/result" component={Result} />
<Redirect from="/" to="/home" />
</Switch>
</BrowserRouter>;
Home.js
const Home = (props) => {
const [id, setId] = useState(0);
const [count, setCount] = useState(0);
const handleCount = () => {
setCount(count + 1);
};
const handleNextId = () => {
if (id >= 3) {
props.history.push({
pathname: "/result",
state: { count: count },
});
} else {
setId(id + 1);
}
};
return (
<div>
<div>id: {id}</div>
<div>count: {count}</div>
<br />
<Page count={handleCount} nextId={handleNextId} />
</div>
);
};
export default Home;
Page.js
const Page = (props) => {
const [check, setCheck] = useState(true);
const handleCheck = () => {
if (check) {
props.count();
}
props.nextId();
};
return (
<button type="text" onClick={handleCheck}>
next
</button>
);
};
export default Page;
答案 0 :(得分:0)
好的,我想出了一种方法,可以通过将props.history.push
移到useEffect
来监视id
的更改。不知道这是否是最佳做法。请让我知道。
Home.js
const Home = (props) => {
const [id, setId] = useState(0);
const [count, setCount] = useState(0);
useEffect(() => {
if (id >= 3) {
props.history.push({
pathname: "/result",
state: { count: count },
});
}
}, [id]);
const handleCount = () => {
setCount(count + 1);
};
const handleNextId = () => {
setId(id + 1);
};
return (
<div>
<div>id: {id}</div>
<div>count: {count}</div>
<br />
<Page count={handleCount} nextId={handleNextId} />
</div>
);
};
export default Home;
答案 1 :(得分:-1)
如果要获取当前值以传递到历史记录:
let currentCount;
setCount(current => {
currentCount = current;
return current;
});
props.history.push({
pathname: "/result",
state: { count: currentCount },
});
还需要增加当前值:
setCount(current => current + 1);
对ID的想法相同。