React data fetch - two stage update [closed] The Next CEO of Stack OverflowHow can I merge properties of two JavaScript objects dynamically?Compare two dates with JavaScriptConvert form data to JavaScript object with jQueryGenerate random number between two numbers in JavaScriptHow does data binding work in AngularJS?How do I update each dependency in package.json to the latest version?Loop inside React JSXWhat do these three dots in React do?Programmatically navigate using react routerHow to update data in the React Lifecycle?

What difference does it make matching a word with/without a trailing whitespace?

Is a linearly independent set whose span is dense a Schauder basis?

Which acid/base does a strong base/acid react when added to a buffer solution?

Is the offspring between a demon and a celestial possible? If so what is it called and is it in a book somewhere?

Does Germany produce more waste than the US?

Early programmable calculators with RS-232

A hang glider, sudden unexpected lift to 25,000 feet altitude, what could do this?

Free fall ellipse or parabola?

How can the PCs determine if an item is a phylactery?

Create custom note boxes

How to coordinate airplane tickets?

Find a path from s to t using as few red nodes as possible

Can a PhD from a non-TU9 German university become a professor in a TU9 university?

What did the word "leisure" mean in late 18th Century usage?

Compensation for working overtime on Saturdays

Is it possible to create a QR code using text?

Does the Idaho Potato Commission associate potato skins with healthy eating?

Would a grinding machine be a simple and workable propulsion system for an interplanetary spacecraft?

How to implement Comparable so it is consistent with identity-equality

Is it reasonable to ask other researchers to send me their previous grant applications?

Calculate the Mean mean of two numbers

How to unfasten electrical subpanel attached with ramset

Airship steam engine room - problems and conflict

How can a day be of 24 hours?



React data fetch - two stage update [closed]



The Next CEO of Stack OverflowHow can I merge properties of two JavaScript objects dynamically?Compare two dates with JavaScriptConvert form data to JavaScript object with jQueryGenerate random number between two numbers in JavaScriptHow does data binding work in AngularJS?How do I update each dependency in package.json to the latest version?Loop inside React JSXWhat do these three dots in React do?Programmatically navigate using react routerHow to update data in the React Lifecycle?










1















I'm fairly new to react and completely stuck on this point.



Say I have a fairly simple app with the following structure:



<App>
|___<Container>
|___<Chart>


Where in <App>, I have an event that modifies the props of <Container> and subsequently fires off a query method in <Container> to a database based off these props, which takes a certain amount of time (e.g. 1 second). Once this query returns, it modifies the state of <Container>, specifically a data property that goes into the props of <Chart>.



Now say I want <Chart> to be able to see what has changed between the previous and new props from <App> (assume these are also passed all the way to <Chart>). What we get is:



App event -> modifies App state -> updates Container based on props -> fires query



But where can I place this 'fire query' function? If I do this in ComponentDidUpdate, it means I lose the previous props since the query modifies the state of <Container>, thus updating <Chart> for a second time, meaning that by the second time around, 'prevProps' will already be the updated props after the event in the <App> element.



What can I do here? I can't see how I can use shouldComponentUpdate here since that fires before the update, and I need the 'update' event to fire so that the <Container> element has a reason to re-fetch the data after the event in <App>.



Any help would be much appreciated!










share|improve this question













closed as off-topic by js1568, Al Foиce ѫ, AndrewL64, Pablo Navarro, jhpratt Mar 7 at 22:18


This question appears to be off-topic. The users who voted to close gave this specific reason:


  • "Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers. See: How to create a Minimal, Complete, and Verifiable example." – js1568, Al Foиce ѫ, AndrewL64, jhpratt
If this question can be reworded to fit the rules in the help center, please edit the question.











  • 1





    You havn't provided any code. Always provide what have you done till now in code when asking so that people have more context stackoverflow.com/help/how-to-ask

    – Hemant Parashar
    Mar 7 at 19:56












  • Let's say you pass propA from App all the way down to Chart. And Chart receives another prop data from container based on fetch result. Are you saying Chart should be able to detect changes in propA?

    – abadalyan
    Mar 7 at 20:10












  • @abadalyan exactly. I think my problem is somewhat covered in this thread, which it seems never really came to a proper conclusion github.com/reactjs/reactjs.org/issues/721

    – tompiler
    Mar 7 at 20:12











  • If you could explain the meaning of props passed down from the App and how you react to the changes in those props it will become clear on how to solve this. It seems like you are trying to use a change in prop as a state for Chart which is not going to work well.

    – abadalyan
    Mar 7 at 20:26












  • So imagine the prop in the App is a filter on a dataset - this filter is then added to the query with the updated props, then the query is fired. Subsequently, the query is returned, triggering another update to Chart from the setState(data: newData) call at the end of the query.

    – tompiler
    Mar 7 at 20:30















1















I'm fairly new to react and completely stuck on this point.



Say I have a fairly simple app with the following structure:



<App>
|___<Container>
|___<Chart>


Where in <App>, I have an event that modifies the props of <Container> and subsequently fires off a query method in <Container> to a database based off these props, which takes a certain amount of time (e.g. 1 second). Once this query returns, it modifies the state of <Container>, specifically a data property that goes into the props of <Chart>.



Now say I want <Chart> to be able to see what has changed between the previous and new props from <App> (assume these are also passed all the way to <Chart>). What we get is:



App event -> modifies App state -> updates Container based on props -> fires query



But where can I place this 'fire query' function? If I do this in ComponentDidUpdate, it means I lose the previous props since the query modifies the state of <Container>, thus updating <Chart> for a second time, meaning that by the second time around, 'prevProps' will already be the updated props after the event in the <App> element.



What can I do here? I can't see how I can use shouldComponentUpdate here since that fires before the update, and I need the 'update' event to fire so that the <Container> element has a reason to re-fetch the data after the event in <App>.



Any help would be much appreciated!










share|improve this question













closed as off-topic by js1568, Al Foиce ѫ, AndrewL64, Pablo Navarro, jhpratt Mar 7 at 22:18


This question appears to be off-topic. The users who voted to close gave this specific reason:


  • "Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers. See: How to create a Minimal, Complete, and Verifiable example." – js1568, Al Foиce ѫ, AndrewL64, jhpratt
If this question can be reworded to fit the rules in the help center, please edit the question.











  • 1





    You havn't provided any code. Always provide what have you done till now in code when asking so that people have more context stackoverflow.com/help/how-to-ask

    – Hemant Parashar
    Mar 7 at 19:56












  • Let's say you pass propA from App all the way down to Chart. And Chart receives another prop data from container based on fetch result. Are you saying Chart should be able to detect changes in propA?

    – abadalyan
    Mar 7 at 20:10












  • @abadalyan exactly. I think my problem is somewhat covered in this thread, which it seems never really came to a proper conclusion github.com/reactjs/reactjs.org/issues/721

    – tompiler
    Mar 7 at 20:12











  • If you could explain the meaning of props passed down from the App and how you react to the changes in those props it will become clear on how to solve this. It seems like you are trying to use a change in prop as a state for Chart which is not going to work well.

    – abadalyan
    Mar 7 at 20:26












  • So imagine the prop in the App is a filter on a dataset - this filter is then added to the query with the updated props, then the query is fired. Subsequently, the query is returned, triggering another update to Chart from the setState(data: newData) call at the end of the query.

    – tompiler
    Mar 7 at 20:30













1












1








1








I'm fairly new to react and completely stuck on this point.



Say I have a fairly simple app with the following structure:



<App>
|___<Container>
|___<Chart>


Where in <App>, I have an event that modifies the props of <Container> and subsequently fires off a query method in <Container> to a database based off these props, which takes a certain amount of time (e.g. 1 second). Once this query returns, it modifies the state of <Container>, specifically a data property that goes into the props of <Chart>.



Now say I want <Chart> to be able to see what has changed between the previous and new props from <App> (assume these are also passed all the way to <Chart>). What we get is:



App event -> modifies App state -> updates Container based on props -> fires query



But where can I place this 'fire query' function? If I do this in ComponentDidUpdate, it means I lose the previous props since the query modifies the state of <Container>, thus updating <Chart> for a second time, meaning that by the second time around, 'prevProps' will already be the updated props after the event in the <App> element.



What can I do here? I can't see how I can use shouldComponentUpdate here since that fires before the update, and I need the 'update' event to fire so that the <Container> element has a reason to re-fetch the data after the event in <App>.



Any help would be much appreciated!










share|improve this question














I'm fairly new to react and completely stuck on this point.



Say I have a fairly simple app with the following structure:



<App>
|___<Container>
|___<Chart>


Where in <App>, I have an event that modifies the props of <Container> and subsequently fires off a query method in <Container> to a database based off these props, which takes a certain amount of time (e.g. 1 second). Once this query returns, it modifies the state of <Container>, specifically a data property that goes into the props of <Chart>.



Now say I want <Chart> to be able to see what has changed between the previous and new props from <App> (assume these are also passed all the way to <Chart>). What we get is:



App event -> modifies App state -> updates Container based on props -> fires query



But where can I place this 'fire query' function? If I do this in ComponentDidUpdate, it means I lose the previous props since the query modifies the state of <Container>, thus updating <Chart> for a second time, meaning that by the second time around, 'prevProps' will already be the updated props after the event in the <App> element.



What can I do here? I can't see how I can use shouldComponentUpdate here since that fires before the update, and I need the 'update' event to fire so that the <Container> element has a reason to re-fetch the data after the event in <App>.



Any help would be much appreciated!







javascript reactjs






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Mar 7 at 19:45









tompilertompiler

14911




14911




closed as off-topic by js1568, Al Foиce ѫ, AndrewL64, Pablo Navarro, jhpratt Mar 7 at 22:18


This question appears to be off-topic. The users who voted to close gave this specific reason:


  • "Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers. See: How to create a Minimal, Complete, and Verifiable example." – js1568, Al Foиce ѫ, AndrewL64, jhpratt
If this question can be reworded to fit the rules in the help center, please edit the question.







closed as off-topic by js1568, Al Foиce ѫ, AndrewL64, Pablo Navarro, jhpratt Mar 7 at 22:18


This question appears to be off-topic. The users who voted to close gave this specific reason:


  • "Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers. See: How to create a Minimal, Complete, and Verifiable example." – js1568, Al Foиce ѫ, AndrewL64, jhpratt
If this question can be reworded to fit the rules in the help center, please edit the question.







  • 1





    You havn't provided any code. Always provide what have you done till now in code when asking so that people have more context stackoverflow.com/help/how-to-ask

    – Hemant Parashar
    Mar 7 at 19:56












  • Let's say you pass propA from App all the way down to Chart. And Chart receives another prop data from container based on fetch result. Are you saying Chart should be able to detect changes in propA?

    – abadalyan
    Mar 7 at 20:10












  • @abadalyan exactly. I think my problem is somewhat covered in this thread, which it seems never really came to a proper conclusion github.com/reactjs/reactjs.org/issues/721

    – tompiler
    Mar 7 at 20:12











  • If you could explain the meaning of props passed down from the App and how you react to the changes in those props it will become clear on how to solve this. It seems like you are trying to use a change in prop as a state for Chart which is not going to work well.

    – abadalyan
    Mar 7 at 20:26












  • So imagine the prop in the App is a filter on a dataset - this filter is then added to the query with the updated props, then the query is fired. Subsequently, the query is returned, triggering another update to Chart from the setState(data: newData) call at the end of the query.

    – tompiler
    Mar 7 at 20:30












  • 1





    You havn't provided any code. Always provide what have you done till now in code when asking so that people have more context stackoverflow.com/help/how-to-ask

    – Hemant Parashar
    Mar 7 at 19:56












  • Let's say you pass propA from App all the way down to Chart. And Chart receives another prop data from container based on fetch result. Are you saying Chart should be able to detect changes in propA?

    – abadalyan
    Mar 7 at 20:10












  • @abadalyan exactly. I think my problem is somewhat covered in this thread, which it seems never really came to a proper conclusion github.com/reactjs/reactjs.org/issues/721

    – tompiler
    Mar 7 at 20:12











  • If you could explain the meaning of props passed down from the App and how you react to the changes in those props it will become clear on how to solve this. It seems like you are trying to use a change in prop as a state for Chart which is not going to work well.

    – abadalyan
    Mar 7 at 20:26












  • So imagine the prop in the App is a filter on a dataset - this filter is then added to the query with the updated props, then the query is fired. Subsequently, the query is returned, triggering another update to Chart from the setState(data: newData) call at the end of the query.

    – tompiler
    Mar 7 at 20:30







1




1





You havn't provided any code. Always provide what have you done till now in code when asking so that people have more context stackoverflow.com/help/how-to-ask

– Hemant Parashar
Mar 7 at 19:56






You havn't provided any code. Always provide what have you done till now in code when asking so that people have more context stackoverflow.com/help/how-to-ask

– Hemant Parashar
Mar 7 at 19:56














Let's say you pass propA from App all the way down to Chart. And Chart receives another prop data from container based on fetch result. Are you saying Chart should be able to detect changes in propA?

– abadalyan
Mar 7 at 20:10






Let's say you pass propA from App all the way down to Chart. And Chart receives another prop data from container based on fetch result. Are you saying Chart should be able to detect changes in propA?

– abadalyan
Mar 7 at 20:10














@abadalyan exactly. I think my problem is somewhat covered in this thread, which it seems never really came to a proper conclusion github.com/reactjs/reactjs.org/issues/721

– tompiler
Mar 7 at 20:12





@abadalyan exactly. I think my problem is somewhat covered in this thread, which it seems never really came to a proper conclusion github.com/reactjs/reactjs.org/issues/721

– tompiler
Mar 7 at 20:12













If you could explain the meaning of props passed down from the App and how you react to the changes in those props it will become clear on how to solve this. It seems like you are trying to use a change in prop as a state for Chart which is not going to work well.

– abadalyan
Mar 7 at 20:26






If you could explain the meaning of props passed down from the App and how you react to the changes in those props it will become clear on how to solve this. It seems like you are trying to use a change in prop as a state for Chart which is not going to work well.

– abadalyan
Mar 7 at 20:26














So imagine the prop in the App is a filter on a dataset - this filter is then added to the query with the updated props, then the query is fired. Subsequently, the query is returned, triggering another update to Chart from the setState(data: newData) call at the end of the query.

– tompiler
Mar 7 at 20:30





So imagine the prop in the App is a filter on a dataset - this filter is then added to the query with the updated props, then the query is fired. Subsequently, the query is returned, triggering another update to Chart from the setState(data: newData) call at the end of the query.

– tompiler
Mar 7 at 20:30












1 Answer
1






active

oldest

votes


















0














Seems to be some confusion around props vs state. Think of state as private to that component. Updating state of container does not mean props are altered.



Think of a light switch. State is on or off. However the home designer may have defined certain properties and passed them on to the interior design team around things like color and shape. These are separate.



Key point is the light switch can have a state of on or off and this does not change the properties of the light switch.



In your example, prop history can be tracked as a part of the container state.






class Container extends React.Component 
constructor(props)
super(props);

this.state =
data: [
[Math.random(), Math.random(), Math.random()]
]



componentDidMount()
setInterval(() => this.setState(
data: [
...this.state.data,
[Math.random(), Math.random(), Math.random()]
]
), 5000)


render()
return (
<Chart data=this.state.data />
);



class Chart extends React.Component
render() []).reduce((total, current) => total + current, 0);
const currentDataSum = (data[1]


ReactDOM.render(<Container />, document.getElementById('root'))

<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>

<div id="root"></div>








share|improve this answer

























  • Thanks - the crucial thing is that I need the older props in the Chart element, as well as the new ones. Because I need to setState when updating the Container element, I lose these 'prevProps', as the setState triggers a new update and on that rerender, the Chart element recieves the props from A after, not before, the event. This is the problem - to update 'twice' but to retain the original props from A for use in the Chart component.

    – tompiler
    Mar 7 at 20:18











  • Sounds like props passed to chart should be an array of data instead of a single instance of data. Calling setState in the Container is not going to change the props that were originally passed to the Container element.

    – Joshua Robinson
    Mar 7 at 20:22












  • It doesn't matter what format the Container's state.data is. I don't think you're understanding my problem - I don't want to change the props of App, I want to record how they have changed. Usually this can be done with ComponentDidUpdate but since that lifecycle method has to be called twice, I'm struggling to see a way...

    – tompiler
    Mar 7 at 20:26











  • If you need the older props in the chart element store them in an array. ComponentDidUpdate is not called in chart component initial render so store initial props on componentDidMount or in the constructor.

    – Joshua Robinson
    Mar 7 at 20:37












  • @tompiler Added example code, where data is an array that grows as fetch occurs, fetch is mocked by a setInterval. This preserves the chart props in the parent. Chart slices off the last two items which would be the previous data and the most current data, displays the sum of each and calculates the difference.

    – Joshua Robinson
    Mar 7 at 21:07


















1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














Seems to be some confusion around props vs state. Think of state as private to that component. Updating state of container does not mean props are altered.



Think of a light switch. State is on or off. However the home designer may have defined certain properties and passed them on to the interior design team around things like color and shape. These are separate.



Key point is the light switch can have a state of on or off and this does not change the properties of the light switch.



In your example, prop history can be tracked as a part of the container state.






class Container extends React.Component 
constructor(props)
super(props);

this.state =
data: [
[Math.random(), Math.random(), Math.random()]
]



componentDidMount()
setInterval(() => this.setState(
data: [
...this.state.data,
[Math.random(), Math.random(), Math.random()]
]
), 5000)


render()
return (
<Chart data=this.state.data />
);



class Chart extends React.Component
render() []).reduce((total, current) => total + current, 0);
const currentDataSum = (data[1]


ReactDOM.render(<Container />, document.getElementById('root'))

<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>

<div id="root"></div>








share|improve this answer

























  • Thanks - the crucial thing is that I need the older props in the Chart element, as well as the new ones. Because I need to setState when updating the Container element, I lose these 'prevProps', as the setState triggers a new update and on that rerender, the Chart element recieves the props from A after, not before, the event. This is the problem - to update 'twice' but to retain the original props from A for use in the Chart component.

    – tompiler
    Mar 7 at 20:18











  • Sounds like props passed to chart should be an array of data instead of a single instance of data. Calling setState in the Container is not going to change the props that were originally passed to the Container element.

    – Joshua Robinson
    Mar 7 at 20:22












  • It doesn't matter what format the Container's state.data is. I don't think you're understanding my problem - I don't want to change the props of App, I want to record how they have changed. Usually this can be done with ComponentDidUpdate but since that lifecycle method has to be called twice, I'm struggling to see a way...

    – tompiler
    Mar 7 at 20:26











  • If you need the older props in the chart element store them in an array. ComponentDidUpdate is not called in chart component initial render so store initial props on componentDidMount or in the constructor.

    – Joshua Robinson
    Mar 7 at 20:37












  • @tompiler Added example code, where data is an array that grows as fetch occurs, fetch is mocked by a setInterval. This preserves the chart props in the parent. Chart slices off the last two items which would be the previous data and the most current data, displays the sum of each and calculates the difference.

    – Joshua Robinson
    Mar 7 at 21:07
















0














Seems to be some confusion around props vs state. Think of state as private to that component. Updating state of container does not mean props are altered.



Think of a light switch. State is on or off. However the home designer may have defined certain properties and passed them on to the interior design team around things like color and shape. These are separate.



Key point is the light switch can have a state of on or off and this does not change the properties of the light switch.



In your example, prop history can be tracked as a part of the container state.






class Container extends React.Component 
constructor(props)
super(props);

this.state =
data: [
[Math.random(), Math.random(), Math.random()]
]



componentDidMount()
setInterval(() => this.setState(
data: [
...this.state.data,
[Math.random(), Math.random(), Math.random()]
]
), 5000)


render()
return (
<Chart data=this.state.data />
);



class Chart extends React.Component
render() []).reduce((total, current) => total + current, 0);
const currentDataSum = (data[1]


ReactDOM.render(<Container />, document.getElementById('root'))

<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>

<div id="root"></div>








share|improve this answer

























  • Thanks - the crucial thing is that I need the older props in the Chart element, as well as the new ones. Because I need to setState when updating the Container element, I lose these 'prevProps', as the setState triggers a new update and on that rerender, the Chart element recieves the props from A after, not before, the event. This is the problem - to update 'twice' but to retain the original props from A for use in the Chart component.

    – tompiler
    Mar 7 at 20:18











  • Sounds like props passed to chart should be an array of data instead of a single instance of data. Calling setState in the Container is not going to change the props that were originally passed to the Container element.

    – Joshua Robinson
    Mar 7 at 20:22












  • It doesn't matter what format the Container's state.data is. I don't think you're understanding my problem - I don't want to change the props of App, I want to record how they have changed. Usually this can be done with ComponentDidUpdate but since that lifecycle method has to be called twice, I'm struggling to see a way...

    – tompiler
    Mar 7 at 20:26











  • If you need the older props in the chart element store them in an array. ComponentDidUpdate is not called in chart component initial render so store initial props on componentDidMount or in the constructor.

    – Joshua Robinson
    Mar 7 at 20:37












  • @tompiler Added example code, where data is an array that grows as fetch occurs, fetch is mocked by a setInterval. This preserves the chart props in the parent. Chart slices off the last two items which would be the previous data and the most current data, displays the sum of each and calculates the difference.

    – Joshua Robinson
    Mar 7 at 21:07














0












0








0







Seems to be some confusion around props vs state. Think of state as private to that component. Updating state of container does not mean props are altered.



Think of a light switch. State is on or off. However the home designer may have defined certain properties and passed them on to the interior design team around things like color and shape. These are separate.



Key point is the light switch can have a state of on or off and this does not change the properties of the light switch.



In your example, prop history can be tracked as a part of the container state.






class Container extends React.Component 
constructor(props)
super(props);

this.state =
data: [
[Math.random(), Math.random(), Math.random()]
]



componentDidMount()
setInterval(() => this.setState(
data: [
...this.state.data,
[Math.random(), Math.random(), Math.random()]
]
), 5000)


render()
return (
<Chart data=this.state.data />
);



class Chart extends React.Component
render() []).reduce((total, current) => total + current, 0);
const currentDataSum = (data[1]


ReactDOM.render(<Container />, document.getElementById('root'))

<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>

<div id="root"></div>








share|improve this answer















Seems to be some confusion around props vs state. Think of state as private to that component. Updating state of container does not mean props are altered.



Think of a light switch. State is on or off. However the home designer may have defined certain properties and passed them on to the interior design team around things like color and shape. These are separate.



Key point is the light switch can have a state of on or off and this does not change the properties of the light switch.



In your example, prop history can be tracked as a part of the container state.






class Container extends React.Component 
constructor(props)
super(props);

this.state =
data: [
[Math.random(), Math.random(), Math.random()]
]



componentDidMount()
setInterval(() => this.setState(
data: [
...this.state.data,
[Math.random(), Math.random(), Math.random()]
]
), 5000)


render()
return (
<Chart data=this.state.data />
);



class Chart extends React.Component
render() []).reduce((total, current) => total + current, 0);
const currentDataSum = (data[1]


ReactDOM.render(<Container />, document.getElementById('root'))

<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>

<div id="root"></div>








class Container extends React.Component 
constructor(props)
super(props);

this.state =
data: [
[Math.random(), Math.random(), Math.random()]
]



componentDidMount()
setInterval(() => this.setState(
data: [
...this.state.data,
[Math.random(), Math.random(), Math.random()]
]
), 5000)


render()
return (
<Chart data=this.state.data />
);



class Chart extends React.Component
render() []).reduce((total, current) => total + current, 0);
const currentDataSum = (data[1]


ReactDOM.render(<Container />, document.getElementById('root'))

<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>

<div id="root"></div>





class Container extends React.Component 
constructor(props)
super(props);

this.state =
data: [
[Math.random(), Math.random(), Math.random()]
]



componentDidMount()
setInterval(() => this.setState(
data: [
...this.state.data,
[Math.random(), Math.random(), Math.random()]
]
), 5000)


render()
return (
<Chart data=this.state.data />
);



class Chart extends React.Component
render() []).reduce((total, current) => total + current, 0);
const currentDataSum = (data[1]


ReactDOM.render(<Container />, document.getElementById('root'))

<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>

<div id="root"></div>






share|improve this answer














share|improve this answer



share|improve this answer








edited Mar 7 at 21:10

























answered Mar 7 at 20:12









Joshua RobinsonJoshua Robinson

1,8201824




1,8201824












  • Thanks - the crucial thing is that I need the older props in the Chart element, as well as the new ones. Because I need to setState when updating the Container element, I lose these 'prevProps', as the setState triggers a new update and on that rerender, the Chart element recieves the props from A after, not before, the event. This is the problem - to update 'twice' but to retain the original props from A for use in the Chart component.

    – tompiler
    Mar 7 at 20:18











  • Sounds like props passed to chart should be an array of data instead of a single instance of data. Calling setState in the Container is not going to change the props that were originally passed to the Container element.

    – Joshua Robinson
    Mar 7 at 20:22












  • It doesn't matter what format the Container's state.data is. I don't think you're understanding my problem - I don't want to change the props of App, I want to record how they have changed. Usually this can be done with ComponentDidUpdate but since that lifecycle method has to be called twice, I'm struggling to see a way...

    – tompiler
    Mar 7 at 20:26











  • If you need the older props in the chart element store them in an array. ComponentDidUpdate is not called in chart component initial render so store initial props on componentDidMount or in the constructor.

    – Joshua Robinson
    Mar 7 at 20:37












  • @tompiler Added example code, where data is an array that grows as fetch occurs, fetch is mocked by a setInterval. This preserves the chart props in the parent. Chart slices off the last two items which would be the previous data and the most current data, displays the sum of each and calculates the difference.

    – Joshua Robinson
    Mar 7 at 21:07


















  • Thanks - the crucial thing is that I need the older props in the Chart element, as well as the new ones. Because I need to setState when updating the Container element, I lose these 'prevProps', as the setState triggers a new update and on that rerender, the Chart element recieves the props from A after, not before, the event. This is the problem - to update 'twice' but to retain the original props from A for use in the Chart component.

    – tompiler
    Mar 7 at 20:18











  • Sounds like props passed to chart should be an array of data instead of a single instance of data. Calling setState in the Container is not going to change the props that were originally passed to the Container element.

    – Joshua Robinson
    Mar 7 at 20:22












  • It doesn't matter what format the Container's state.data is. I don't think you're understanding my problem - I don't want to change the props of App, I want to record how they have changed. Usually this can be done with ComponentDidUpdate but since that lifecycle method has to be called twice, I'm struggling to see a way...

    – tompiler
    Mar 7 at 20:26











  • If you need the older props in the chart element store them in an array. ComponentDidUpdate is not called in chart component initial render so store initial props on componentDidMount or in the constructor.

    – Joshua Robinson
    Mar 7 at 20:37












  • @tompiler Added example code, where data is an array that grows as fetch occurs, fetch is mocked by a setInterval. This preserves the chart props in the parent. Chart slices off the last two items which would be the previous data and the most current data, displays the sum of each and calculates the difference.

    – Joshua Robinson
    Mar 7 at 21:07

















Thanks - the crucial thing is that I need the older props in the Chart element, as well as the new ones. Because I need to setState when updating the Container element, I lose these 'prevProps', as the setState triggers a new update and on that rerender, the Chart element recieves the props from A after, not before, the event. This is the problem - to update 'twice' but to retain the original props from A for use in the Chart component.

– tompiler
Mar 7 at 20:18





Thanks - the crucial thing is that I need the older props in the Chart element, as well as the new ones. Because I need to setState when updating the Container element, I lose these 'prevProps', as the setState triggers a new update and on that rerender, the Chart element recieves the props from A after, not before, the event. This is the problem - to update 'twice' but to retain the original props from A for use in the Chart component.

– tompiler
Mar 7 at 20:18













Sounds like props passed to chart should be an array of data instead of a single instance of data. Calling setState in the Container is not going to change the props that were originally passed to the Container element.

– Joshua Robinson
Mar 7 at 20:22






Sounds like props passed to chart should be an array of data instead of a single instance of data. Calling setState in the Container is not going to change the props that were originally passed to the Container element.

– Joshua Robinson
Mar 7 at 20:22














It doesn't matter what format the Container's state.data is. I don't think you're understanding my problem - I don't want to change the props of App, I want to record how they have changed. Usually this can be done with ComponentDidUpdate but since that lifecycle method has to be called twice, I'm struggling to see a way...

– tompiler
Mar 7 at 20:26





It doesn't matter what format the Container's state.data is. I don't think you're understanding my problem - I don't want to change the props of App, I want to record how they have changed. Usually this can be done with ComponentDidUpdate but since that lifecycle method has to be called twice, I'm struggling to see a way...

– tompiler
Mar 7 at 20:26













If you need the older props in the chart element store them in an array. ComponentDidUpdate is not called in chart component initial render so store initial props on componentDidMount or in the constructor.

– Joshua Robinson
Mar 7 at 20:37






If you need the older props in the chart element store them in an array. ComponentDidUpdate is not called in chart component initial render so store initial props on componentDidMount or in the constructor.

– Joshua Robinson
Mar 7 at 20:37














@tompiler Added example code, where data is an array that grows as fetch occurs, fetch is mocked by a setInterval. This preserves the chart props in the parent. Chart slices off the last two items which would be the previous data and the most current data, displays the sum of each and calculates the difference.

– Joshua Robinson
Mar 7 at 21:07






@tompiler Added example code, where data is an array that grows as fetch occurs, fetch is mocked by a setInterval. This preserves the chart props in the parent. Chart slices off the last two items which would be the previous data and the most current data, displays the sum of each and calculates the difference.

– Joshua Robinson
Mar 7 at 21:07






Popular posts from this blog

Save data to MySQL database using ExtJS and PHP [closed]2019 Community Moderator ElectionHow can I prevent SQL injection in PHP?Which MySQL data type to use for storing boolean valuesPHP: Delete an element from an arrayHow do I connect to a MySQL Database in Python?Should I use the datetime or timestamp data type in MySQL?How to get a list of MySQL user accountsHow Do You Parse and Process HTML/XML in PHP?Reference — What does this symbol mean in PHP?How does PHP 'foreach' actually work?Why shouldn't I use mysql_* functions in PHP?

Compiling GNU Global with universal-ctags support Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern) Data science time! April 2019 and salary with experience The Ask Question Wizard is Live!Tags for Emacs: Relationship between etags, ebrowse, cscope, GNU Global and exuberant ctagsVim and Ctags tips and trickscscope or ctags why choose one over the other?scons and ctagsctags cannot open option file “.ctags”Adding tag scopes in universal-ctagsShould I use Universal-ctags?Universal ctags on WindowsHow do I install GNU Global with universal ctags support using Homebrew?Universal ctags with emacsHow to highlight ctags generated by Universal Ctags in Vim?

Add ONERROR event to image from jsp tldHow to add an image to a JPanel?Saving image from PHP URLHTML img scalingCheck if an image is loaded (no errors) with jQueryHow to force an <img> to take up width, even if the image is not loadedHow do I populate hidden form field with a value set in Spring ControllerStyling Raw elements Generated from JSP tagds with Jquery MobileLimit resizing of images with explicitly set width and height attributeserror TLD use in a jsp fileJsp tld files cannot be resolved