2

I am setting a state into child component on event perform and want to sent this to Parent component. I searched for this on SO. But still didn't found any way to do this.

Let say i have a parent component Home, and have child component User. I am performing some event in User component, and at that time, i want to pass data to Home component. How can i do this?

Below is my code:

/* Parent component */

import React, { Component } from 'react';
import User from './user';

class Home extends React.Component{
    constructor(props){
        super(props)
        this.state = {
           isReportSent: false
        }   
    }
    render(){
        <Switch>
           <Route exact path="/" component={User}/>
        </Switch>
    }
}

/* child component */
class User extends React.Component{
    constructor(props){
        super(props)
    }
    render(){

    }
}

Note: My parent component is Routing component, in which i am routing my child component on particular path. So can't pass any function to child component.

5
  • Why don't you call the API in the parent itself? Commented Dec 24, 2018 at 11:41
  • No, I can't. Please tell me, if you know. Commented Dec 24, 2018 at 11:42
  • This is the case for reactjs.org/docs/lifting-state-up.html Commented Dec 24, 2018 at 11:42
  • Please paste your child and parent component code so I can help you. Commented Dec 24, 2018 at 11:42
  • Why do not you pass the callback function as extra prop from parent to child and call that callback from the child component Commented Dec 24, 2018 at 13:48

5 Answers 5

3
import React, { Component } from "react";
class Home extends Component {
    constructor(props) {
      super(props);
      this.state = {};
    }
    onChildAPICall = result => {
      console.log(result);
    };

    render() {
      return <User onAPICall={this.onChildAPICall} />;
    }
}

class User extends Component {
   constructor(props) {
     super(props);
     this.state = {};
     this.API = "https://apicall";
   }

   makeAnAPICall = async () => {
     let result = await fetch(this.API);
     this.props.onAPICall(result);
   };

   render() {
     return <button onClick={this.makeAnAPICall}>API Call</button>;
   }
}

export default Home;
Sign up to request clarification or add additional context in comments.

Comments

1

Something like this would work. I'm not sure if the below is 100% functioning as I just wrote it quickly but the idea is to pass down setState() as a prop from parent to child. So when child calls setState from props it's setting state in the parent component.

 class Home extends React.Component {
  constructor (props) {
    super(props)
    this.state = {
      data: []
    }
  }

  render () {
    <ChilComponent setState={this.setState} />
  }
}

const User = async ({ setState }) => {
  const receivedData = await getDataHowever(params)
  setState({
    data: receivedData
  })
  return (
  <p>Got data!</p>
  )
}

1 Comment

Yes, this is the easiest way -- pass a function to the child component as props that sets state. The only other way is to get redux or some state management involved. This should be the accepted answer.
0

You can call callback function of parent from child component and in parent you can set the state based on callback response.

import React, { Component } from "react";

class Home extends Component {
    constructor(props) {
      super(props);
      this.state = { }
    }
    setStateOfParent= result => {
      this.setState({result : result});
    }

    render() {
      return <User setStateOfParent={this.setStateOfParent} />;
    }
}

class User extends Component {
   constructor(props) {
     super(props);
     this.state = {};
     this.API = "https://apicall";
   }

   makeAnAPICall = async () => {
     let result = await fetch(this.API);
     this.props.setStateOfParent(result);
   };

   render() {
     return <button onClick={this.makeAnAPICall}>API Call</button>;
   }
}

export default Home;

1 Comment

My parent component is, where i am performing routing, so as you call child component inside parent like: <Child component>. I am not doing this. Do you have another option?
0

Your explanation is not all clear what you want to acheive but as a simple pattern you can pass the callback prop to the child component using render method of react router

Parent Component

<Route exact path="/" render={(props) => <User {...props} callback={this.callback} />}/>

Child Class

this.props.callback(data)

Comments

0

@user10742206 The best way is to create an independent component and include it as a child in any parent component. Then you can pass a callback function from parent and child can use it to send back any data to parent.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.