[英]How to reset state variable value to its initial value always when a component is rendered in React
I have a React component which is like a graph where you can click on different nodes to get additional info.我有一个 React 组件,它就像一个图表,您可以在其中单击不同的节点以获取更多信息。 I have a boolean state variable ( clickedNode ) in this component to determine if I need to render another component (basically tables with info) when a node is clicked.我在此组件中有一个布尔状态变量( clickedNode ),以确定单击节点时是否需要呈现另一个组件(基本上是带有信息的表)。 So from this graph component, I'm calling this table component and in the table component I have another state variable ( clickedTableRow ).所以从这个图形组件中,我调用了这个表格组件,在表格组件中我有另一个状态变量( clickedTableRow )。 Basically, I want this state variable to initialize to false always when this table component is rendered.基本上,我希望这个状态变量在呈现这个表组件时总是初始化为 false。 I have some logic which sets this variable to true in table component but how can I initialize this variable to false always when table component is called from the graph component?我有一些逻辑可以在表格组件中将此变量设置为 true,但是当从图形组件调用表格组件时,如何始终将此变量初始化为 false?
For reference, this is the template for my code:作为参考,这是我的代码的模板:
const Graph=()=>{
//graph rendering code
{clickedNode?(<div> <Table /> </div>):("")}
}
const Table = props => {
let[clickedTableRow,setTableRowClicked]=useState(false) // I want clickedTableRow to be initialized to false always
//rest of the table code
}
您可以使用常规变量而不是状态:让 clickedTableRow = false
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.