How do I send updating data to a child component. React


If I have an object key values that we assume change due to some input. How do I update the child component that takes those values and displays them. When I send the values as props to the child component they obviously don’t update and stay as the initial value set in tempData. So how would I prompt the child props to resend or update?

I already have functions that fetch the data and put it all into its places in the tempData object. Am I going about this wrong by setting the data in an Object?

const Parent = () => {

const tempData = {
  location: "",
  weather: "",
  temperature: "",
};

return (
    <>
      <System onChange={(value) => urlHandler(value)} />
      <City onChange={(value) => setCity(value)} />
      <Data
        location={tempData.location}
        weather={tempData.location}
        temperature={tempData.temperature}
      />

</>
};

const Child = (props) => {

return(
<>
<div>location: {props.location}</div>
<div>temperature: {props.temperature}</div>
</>
)

};

Source: JavaSript – Stack Overflow

November 21, 2021
Category : News
Tags: javascript | react-component | reactjs

Leave a Reply

Your email address will not be published. Required fields are marked *

Sitemap | Terms | Privacy | Cookies | Advertising

Senior Software Developer

Creator of @LzoMedia I am a backend software developer based in London who likes beautiful code and has an adherence to standards & love's open-source.