简体   繁体   English

在反应中将 state 从子组件传递到父组件

[英]Passing state from child to parent component in react

I have a parent and child component, i want to pass the state(share) from the child component to the parent one in the props(isChecked), how can this be done?我有一个父子组件,我想将状态(共享)从子组件传递到道具(isChecked)中的父组件,这怎么办?

export class Form extends React.Component<FormProps, {
    ...,
    isChecked?: boolean}> {
constructor(props: FormProps) {
    super(props);

    this.state = {
      ...,
      isChecked:false
    };
 private checkField = () => {
           const checked = this.state.isChecked
           console.log('check '+checked)
    }
 render() {

}
return <div><Input name="Name" required={true} isChecked={this.state.isChecked}></div>
}

export function Input(props: React.PropsWithChildren<{name: string, required?: boolean, isChecked?:boolean}>) {
 
  const [share, setShare] = useState(false);
 
  return <Fragment>
    <div>
      {props.name}
      <span onClick={(event) => {setShare(!share);props.isChecked = share;}}>Selection {props.required ? '(Required)': ''}</span>
    </div>
<div className="btn btn--dark btn--margin-left" onClick= 
     {this.checkField}> Verify</div>
  </Fragment>;
}

For Child to Parent Interaction, you should pass a function as prop to the child from parent..对于孩子与父母的互动,您应该将 function 作为 prop 从父母那里传递给孩子。

export class Form extends React.Component<FormProps, {
        ...,
        isChecked?: boolean}> {
    constructor(props: FormProps) {
        super(props);
    
        this.state = {
          ...,
          isChecked:false
        };
     render() {
    
       return <div>
               <Input 
                 name="Name" 
                 required={true} 
                 isChecked={this.state.isChecked}
                 handleChecked={
                 (isChecked: boolean) => this.setState({isChecked})
                 }
                />
             </div>
    }
    

    export function Input(props: React.PropsWithChildren<{name: string, required?: boolean, isChecked?:boolean, handleChecked?: (isChecked: boolean) => void}>) {
     
      const [share, setShare] = useState(false);
     
      return <Fragment>
        <div>
          {props.name}
          <span onClick={
              (event) => {
                  setShare(!share);
                  if(handleChecked) {props.handleChecked(share);}
          }}>Selection {props.required ? '(Required)': ''}</span>
        </div>
      </Fragment>;
    }

I guess you want to use share in your parent component and if that's the case you must set the state in the parent and pass it to your child as a prop just as you did with isChecked .我想您想在父组件中使用share ,如果是这种情况,您必须在父组件中设置 state 并将其作为道具传递给您的孩子,就像您对isChecked所做的那样。

Very easy to do you just need to pass a handleChange prop to your child component from the parent component and then utilize it in the child component.很容易做到,你只需要将handleChange prop 从父组件传递给你的子组件,然后在子组件中使用它。

Checkout this code-sandbox检查此代码沙箱

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

相关问题 React,将状态从子组件传递给父组件 - React, passing state from a Child component to the Parent 反应:将状态从孩子传递给孩子到父母 - React: Passing state from child to child to parent 反应 - 我将 state 从子组件传递给父组件,但 state 值在一个更新周期内关闭 - React - I am passing state from a child to a parent component but the state value is off by one update cycle 使用 React.createContext 将数据从父组件 state 传递到子组件 - passing data from the parent component state to the child component using React.createContext 在React中从父组件到子组件共享状态 - Sharing state from parent to child component in React 在React中将状态从子组件更新为父组件 - Update the state from child to parent component in React React:无法通过将函数传递给更新它的子组件来更新父状态 - React: unable to update parent state by passing function to child component that updates it 反应:将 state 传递给子组件 - React: Passing state to child component 在 React Native 中,将 state 从子组件传递到要渲染的 App 组件 - Passing state from child component to the App component to be renders , in React Native 反应:将图像从父组件中的 this.state 传递给子组件 - React: Pass image from this.state in parent component to child component
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM