简体   繁体   English

如何在 React 中渲染组件时始终将状态变量值重置为其初始值

[英]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.

相关问题 如何在渲染或显示组件时重置 state? - How to reset the state in react when component is rendered or shown? React-native,firestore:当实例变量改变其值时如何更新组件状态 - React-native, firestore: how to update component state when an instance variable changes its value React:如何避免使用钩子重新渲染组件,该钩子始终返回相同的值但会改变其内部状态 - React: how to avoid re-rendering a component with a hook that returns always the same value but changes its inner state 使用A-frame状态组件时如何重置为初始状态? - How to reset to initial state when using A-Frame `state` component? 如何将反应通量存储重置为其初始状态? - How do I reset the react flux store to its initial state? 如何在反应中将组件重置为其初始状态 - How can I reset a component to it's initial state in react 如何将递归渲染组件的值设置为 state? - How to set value to state for recursive rendered component? 如果更改,如何将值重置为其原始状态 - How to reset a value to its original state if changed React Hooks:状态总是设置回初始值 - React Hooks: State always setting back to initial value 带有 useState 的自定义反应钩子始终重置为其初始值 - Custom react hook with useState always resetting to its initial value
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM