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?
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
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
|
show 3 more comments
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
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
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 passpropA
fromApp
all the way down toChart
. AndChart
receives another propdata
from container based on fetch result. Are you sayingChart
should be able to detect changes inpropA
?
– 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
|
show 3 more comments
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
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
javascript reactjs
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
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
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 passpropA
fromApp
all the way down toChart
. AndChart
receives another propdata
from container based on fetch result. Are you sayingChart
should be able to detect changes inpropA
?
– 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
|
show 3 more comments
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 passpropA
fromApp
all the way down toChart
. AndChart
receives another propdata
from container based on fetch result. Are you sayingChart
should be able to detect changes inpropA
?
– 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
|
show 3 more comments
1 Answer
1
active
oldest
votes
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>
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
|
show 1 more comment
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
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>
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
|
show 1 more comment
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>
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
|
show 1 more comment
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>
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>
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
|
show 1 more comment
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
|
show 1 more comment
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
fromApp
all the way down toChart
. AndChart
receives another propdata
from container based on fetch result. Are you sayingChart
should be able to detect changes inpropA
?– 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