0

I am trying to send a variable data from my child class to my parent class.

Doubts

  1. My child is a functional component as a .js file and my parent is a class component as a .tsx file. (was wondering if this would be an issue)

App.Component(.Tsx file)

I've updated my App component with state to pass to child

    import React from "react";
    import FieldData from "./FieldData";
    
    class App extends React.Component {
      constructor(props) {
        super(props);
        this.state = {
          childData: "this is the input for now"
        };
        this.handleData = this.handleData.bind(this);
      }
    
      handleData(dataValue) {
        this.setState({ data: dataValue });
      };
    
      render() {
        return (
          <div>
            Hello passing data from child to parent
            {/* Want to overwrite "this.state.childData" with data from child class */}
            <FieldData handleData ={this.handleData } />
            here is output {this.state.childData}
          </div>
        );
      }
    }
    
    export default App;
    

I've updated the parent class. But I am not sure what to do in the child class. I am not sure how to set the constructor or call the props in a function component. I understand that we cannot set constructor in stateless function component. (I am able to do so if the child is a class component). But I am using hooks, is the reason why I am using function child.

Sample Child functional component(.js file)

I have a function in the child class that print out json object array. I want to pass this "convertedData" value back to the parent class.

const PriceTotal = ({ control }) => {
  const test = useWatch({
    control,
    name: `items`
  });

  if (test) {
    const convertedData = test.map((element) => {
      Object.keys(element).forEach((key) => {
        // check value is exist and parse value string to int
        if (element[key].value)
          element[key].value = parseFloat(element[key].value);
      });
      return element;
    });

    console.log(convertedData);
    // Want to send convertedData back to parent
  }

  return null;
};

I provided a sandbox Below

Sandbox

JayC
  • 2,144
  • 8
  • 42
  • 77
  • Does this answer your question? [How to pass data from child component to its parent in ReactJS?](https://stackoverflow.com/questions/38394015/how-to-pass-data-from-child-component-to-its-parent-in-reactjs) – Daniel Geffen Jan 12 '21 at 20:02
  • @DanielGeffen I followed that post already. I've updated my sandbox. When following the steps I see issue when passing props (in parent to child). Mentioned in my post, if I have .tsx as parent and .js as child does this pose an issue? – JayC Jan 12 '21 at 20:12
  • No it shouldn't be different. – Daniel Geffen Jan 12 '21 at 20:25
  • why not just use a regular utility function instead of a component that does a bunch of data processing and always renders `null`? – Dan O Jan 12 '21 at 23:40
  • @DanO I am not sure how to do so. I followed a tutorial on using react-hook-form and I ended up with this. and I need the array field generated from this child to my parent – JayC Jan 12 '21 at 23:43
  • Is this what you had in mind? https://codesandbox.io/s/sandboxchildtoparent-forked-5nxvb – codemonkey Jan 12 '21 at 23:54
  • @codemonkey I don't see how this is passing data to the parent class. My assumption is that "this.state'childData" in the parent class should be updated. Here is another sandbox that does this but this example has a class parent and class child. https://codesandbox.io/s/ecstatic-meninsky-ubwkr?file=/src/App.js – JayC Jan 12 '21 at 23:59
  • Ah you want it to be sent all the way back to **App**. Ok. check it out now: https://codesandbox.io/s/sandboxchildtoparent-forked-5nxvb – codemonkey Jan 13 '21 at 00:12
  • 1
    Your challenge is not passing `convertedData` back to parent, that part is easy. The trick is you obviously want to update your parent's state with it, which will trigger another render, which, in turn, will trigger yet another call to parent thus creating an endless loop of renders. – codemonkey Jan 13 '21 at 00:14
  • Got it. so with the method I am going for it's not possible to set it as a state as it will continue to render. I think this is what I am looking for. let me try on my end – JayC Jan 13 '21 at 00:16
  • It's certainly possible, you will just need to check if the incoming `convertedData` has already been applied to your state. If so, do not set it again. – codemonkey Jan 13 '21 at 00:19
  • convertedData is onChange basically any change input it will update that value. So my understanding, based on what you said is. We cannot continuously keep setting the state. But if I have a final form of "convertedData" Then I can set the state no issues. – JayC Jan 13 '21 at 00:26
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/227239/discussion-between-jesse-and-codemonkey). – JayC Jan 13 '21 at 00:37

0 Answers0