【发布时间】:2022-01-17 10:56:48
【问题描述】:
当我在另一个组件上调用changeArea 方法时,我想更改区域(元素)。
我想这样做。
首先,App.js
export default function App(props) {
const [area, setArea] = React.useState(<><Button/><Button/></>)
const changeArea = (element) => {
setArea(element);
}
return (
<div>
{<area/>}
<ChildApp changeArea={changeArea}/>
</div>
);
}
还有,ChildApp.js
export default function ChildApp(props) {
// I want do call to change the area.
props.changeArea(<></Select></>);
…
}
无论如何,这段代码不起作用。
Error
Error: Maximum update depth exceeded. This can happen when a component repeatedly calls setState inside componentWillUpdate or componentWillUpdate or componentDidUpdate. React limits the number of nested updates to prevent infinite loops.
PS。这是对我想做的方式的简化。
【问题讨论】:
标签: reactjs react-hooks react-functional-component