Skip to content Skip to sidebar Skip to footer

How To Pass Value From A Child Functional Component To Parent Class Component?

I have a parent class component and a child functional component. How can we pass value from this type of child component to parent component - I have seen some examples that passe

Solution 1:

Parent component

import React from "react";
import ChildComponent from "./ChildComponent/ChildComponent";

class ParentComponent extends React.Component {
    constructor(props) {
        super(props);
        this.state = {
            data: [] // An index value is in this state
        };
    }

    handleClick = (value) => {
       // handle changes from child
       console.log(value)
    }
    render(){
        return(
            <React.Fragment>
             // 1. we pass function for child on props
                <ChildComponent handleClick={this.handleClick} indexval = {this.state.data.index} />                                
            </React.Fragment>
        )
    }
}

export default ParentComponent; 

Child component

import React from 'react';;
import Button from './Button/Button';

function ChildComponent(props) {
    const newVal=(index) => {
        let retVal = index + 1;
        // 2. do calculations and send it to parent
        props.handleClick(retVal);
    }
    return (
        <React.Fragment>
            <Button onClick={() => newVal(props.index)}>Click</Button>
        </React.Fragment>
    )
}

Solution 2:

This is sample to show how you can work with parent and child component.

class Parent extends React.Component {
  state={name:"Hello"}
  handleClick = () => {
    this.setState({name:"Welcome to the world of React."})
  };
  render() {
    return (
      <div className="App">
        <Child name={this.state.name} handleClick={this.handleClick} />
      </div>
    );
  }
}
function Child({name, handleClick}){
    return (
      <div>
       <h2>{name}</h2>
       <button onClick={handleClick}>Change</button>
      </div>
    )
  }

Solution 3:

You can pass a dispatcher to the child component which in turn can call this dispatcher to pass the value you want to:

Parent class component:

import React from "react";
import ChildComponent from "./ChildComponent/ChildComponent";

class ParentComponent extends React.Component {
    constructor(props) {
        super(props);
        this.state = {
            data: [] // An index value is in this state
        };
        this.updateParentState = this.updateParentState.bind(this);
    }
    // dispatcher to pass as a prop
    updateParentState(val){
      this.setState({index:val});
    }

    render(){
        //can console log the return value from the ChildComponent here if needed 
        return(
            <React.Fragment>
                <ChildComponent 
                      indexval = {this.state.data.index} 
                      updateParent={this.updateParentState} />
            </React.Fragment>
        )
    }
}

export default ParentComponent; 

Child component:

import React from 'react';
import Button from './Button/Button';

function ChildComponent(props) {

    const newVal=() => {
        return props.index + 1; //do it this way
    }
    return (
        <React.Fragment>
            <Button onClick={() => props.updateParent(newVal())}>Click</Button>
        </React.Fragment>
    )
}
export default ChildComponent;

Post a Comment for "How To Pass Value From A Child Functional Component To Parent Class Component?"