简体   繁体   English

如何从类组件(父)更改功能组件(子)中的 useState

[英]How to change useState in functional component (child) from class component (parent)

In my parent class I have the following在我的父类中,我有以下内容

<div>
  <button onClick(OPEN MODAL)>Open Modal<button/>
  <ModalReply /> // default to be closed at first
</div>

ModalReply is the functional component ModalReply 是功能组件

function ModalReply(props) {
  const [modalIsOpen, setModalIsOpen] = useState(false)

<div>
  <Modal 
    isOpen={modalIsOpen}
    onRequestClose={() => setModalIsOpen(false)}
  >
   <h1>Test</h1>
  </Modal>
</div>

From my parent class I want to access the modal state component and set the useState --> setModalIsOpen to true从我的父类中,我想访问模态状态组件并将 useState --> setModalIsOpen 设置为 true

set the modalState in the parent and pass them as a props of the child component.在父组件中设置 modalState 并将它们作为子组件的道具传递。

in parent在父母

  const [modalIsOpen, setModalIsOpen] = useState(false)

  const toggleModal = (val) => setModalIsOpen(val)

...

  <ModalReply modalisOpen={modalIsOpen} toggleModal={toggleModal}/>

in child在孩子

function ModalReply(props) {

<div>
  <Modal 
    isOpen={props.modalIsOpen}
    onRequestClose={() => props.toggleModal(false)}
  >
   <h1>Test</h1>
  </Modal>
</div>

Move the state to the parent, make your ModalReply stateless.将状态移动到父级,使您的 ModalReply 无状态。

const ModalReply = ({ isOpen, onClose }) => (
  <div>
    <Modal isOpen={isOpen} onRequestClose={onClose}>
      <h1>Test</h1>
    </Modal>
  </div>
);

export default function App() {
  const [isOpen, setIsOpen] = useState(false);

  const toggle = setIsOpen((oldIsOpen) => !oldIsOpen);

  return (
    <div className="App">
      <button onClick={toggle}>Open Modal</button>
      <ModalReply isOpen={isOpen} onClose={toggle} />
    </div>
  );
}

React data flow is unidirectional, parents can't directly modify the state of child components. React 数据流是单向的,父组件不能直接修改子组件的状态。 To achieve what you want, you need to either:要实现您想要的,您需要:

  • Move the state to the parent component and pass the data as props to the child component.将状态移动到父组件并将数据作为道具传递给子组件。
  • Use a state manager like redux or mobx使用像 redux 或 mobx 这样的状态管理器
  • Use context api使用上下文 API

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在功能组件中从父组件更改子组件 state - How to change the child state component from parent in functional component 如何将值从子功能组件传递给父类组件? - How to pass value from a child functional component to parent class component? 如何使用 useState - hook 将 class 组件(输入,条件)更改为功能组件? - How to change a class component (input, conditional) to functional component with useState - hook? 如何从父级到子级使用功能组件? - How to use functional component from parent to child? 将数据从子 class 组件发送到反应中的父功能组件 - Paasing data from a child class component to a parent functional component in react 从父功能组件调用 class 子组件方法 - Call class child component method from parent functional component 将 UseState 从子组件传递到父组件? - Passing UseState from child component to parent component? 如何使用 useState React hook 更改子组件中父组件的值 - How to change a value from parent component in a child component with useState React hook 如何从不是父或子的另一个组件更改功能组件的 state? - How to change functional component's state from another component that is not a parent or a child? 如何从 React 中的子组件更新父组件(功能) - How to update Parent component from Child component in React (Functional)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM