简体   繁体   English

如何使用 React 中的 Hooks 在两个子组件中更改父 state?

[英]How can I change parent state in two children components using Hooks in React?

I have a Manager component which is parent and has state of mode.我有一个 Manager 组件,它是父组件,并且具有 state 模式。
It conditionally renders two diffrent components related to their state,它有条件地渲染两个与其 state 相关的不同组件,
I want when user clicks on Add button in child component, the state of parent changes.我希望当用户单击子组件中的添加按钮时,父组件的 state 会发生变化。
also vica versa for the other sibling component.对于其他同级组件,反之亦然。
can you help me to solve my proble, I mean how should I change parent state in children components with clicking on button.你能帮我解决我的问题吗,我的意思是我应该如何通过单击按钮更改子组件中的父 state。

if you help me in one children i will do the same for other one.如果你帮助我一个孩子,我也会为另一个孩子做同样的事情。

this is parent component:这是父组件:

import {React, useState} from 'react'
import BasePage from './BasePage'
import AddRecordForm from './AddRecordForm'

function Manager() {
    const [mode, setMode] = useState('base-page');
    
    function handleChangePageClick(Mode) {
        setMode(Mode);
    }
    return (
        <div>
            <h1>My PhoneBook</h1>
            {mode === 'base-page' ? <BasePage onClick = {handleChangePageClick} /> : <AddRecordForm onClick = {handleChangePageClick} />}
        </div>
    );
}

export default Manager

and this is one of its children:这是它的一个孩子:

import {React, useState} from 'react'
import Manager from './Manager'

function BasePage() {
    return (
        <div>
            <fieldset>
        <legend>Search</legend>
        <input placeholder = "Enter name to search" type = "text" />
    </fieldset>
            <br />
        <button onClick>Add</button>
        </div>
    );
}

export default BasePage

Just use the handler you're passing to the child, something like this:只需使用您传递给孩子的处理程序,如下所示:

import {React, useState} from 'react'
import Manager from './Manager'

function BasePage({ onClick }) {
    return (
        <div>
            <fieldset>
        <legend>Search</legend>
        <input placeholder = "Enter name to search" type = "text" />
    </fieldset>
            <br />
        <button onClick={() => onClick('add-record-form')}>Add</button>
        </div>
    );
}

export default BasePage

Instead of adding an onClick event to the component like you're doing here:而不是像您在这里所做的那样向组件添加 onClick 事件:

<BasePage onClick={handleChangePageClick} />

pass down the function as a prop:传递 function 作为道具:

<BasePage handleClick={handleChangePageClick} />

The child component can then call that function onClick :然后子组件可以调用 function onClick

function BasePage({ handleClick }) {
  return (
    <div>
      <fieldset>
        <legend>Search</legend>
        <input placeholder = "Enter name to search" type = "text" />
      </fieldset>
      <br />
      <button onClick={handleClick}>Add</button>
    </div>
  );
}

i suggest you to use the useContext hook or a callback function given by the parent in the props.我建议你使用 props 中父级给出的useContext钩子或回调 function。 For the useContext hook, it will put a data available for the component and all of his children.对于 useContext 钩子,它将为组件及其所有子组件提供可用的数据。

Also by vice versa, rendering a parent component will automatically re-render the child component.反之亦然,渲染父组件将自动重新渲染子组件。

This link will help you这个链接可以帮助你

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM