How can I render a child component or parallel component when a state is set in ReactJS?ReactJS - Does render get called any time “setState” is called?After updating props, this.props is always === nextProps / prevPropsWhat is the difference between state and props in React?When should ReactJS components make AJAX calls to update state from props?Invariant Violation: Objects are not valid as a React childWhy do we need middleware for async flow in Redux?ReactJs - Redux State is not updating in a dropdown in the child componentHow to re-render child component?My react component isn't rendering my state when i receive a response from socket.ioIn reactjs how to update component state when mapStateToProps returns the new prop
Is there some sort of French saying for "a person's signature move"?
How can I hint that my character isn't real?
What is the purpose of the rotating plate in front of the lock?
If every star in the universe except the Sun were destroyed, would we die?
Is it right to use the ideas of non-winning designers in a design contest?
How to apply a register to a command
Sinning and G-d's will, what's wrong with this logic?
Putting future professor position on CV
Project Euler Problem 45
How to interpret or parse this confusing 'NOT' and 'AND' legal clause
Examples where "thin + thin = nice and thick"
How strong is aircraft-grade spruce?
Is mountain bike good for long distances?
Why can't some airports handle heavy aircraft while others do it easily (same runway length)?
"syntax error near unexpected token" after editing .bashrc
Should I tip on the Amtrak train?
Can taking my 1-week-old on a 6-7 hours journey in the car lead to medical complications?
First Number to Contain Each Letter
Contractor cut joist hangers to make them fit
Do 643,000 Americans go bankrupt every year due to medical bills?
What is the "Brake to Exit" feature on the Boeing 777X?
Fantasy Military Arms and Armor: the Dwarven Grand Armory
Project Euler problem #112
What makes an ending "happy"?
How can I render a child component or parallel component when a state is set in ReactJS?
ReactJS - Does render get called any time “setState” is called?After updating props, this.props is always === nextProps / prevPropsWhat is the difference between state and props in React?When should ReactJS components make AJAX calls to update state from props?Invariant Violation: Objects are not valid as a React childWhy do we need middleware for async flow in Redux?ReactJs - Redux State is not updating in a dropdown in the child componentHow to re-render child component?My react component isn't rendering my state when i receive a response from socket.ioIn reactjs how to update component state when mapStateToProps returns the new prop
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
How can I render a child component or parallel component when a state is set?
Main component contains the following code
editValue(data)
console.log("edited")
this.setState( edits:data , function()
console.log("edited value...")
)
render()
return(
<div>
<TextComponent edit=this.editValue.bind(this)/>
<AreaComponent editedValue = this.state.edits/>
</div>
)
TextComponent:
trigger = (data) =>
this.props.edit(data)
render()
return (
<div>
<p onClick=this.trigger.bind(this,'clicked')>Click</p>
</div>
)
AreaComponent:
componentDidUpdate()
console.log("update - component")
componentWillReceiveProps(nextProps)
console.log('receive - component');
Here when I click the <p> I got my console as following
edited
receive - component
update - component
edited value...
What my issue is the edited value that I set in state is not reflecting in my AreaComponent. All the componentDidUpdate() and componentWillReceiveProps() are triggered before setting the state. I want to get the state value inside my next component. What changes should I do in this code?
reactjs
add a comment |
How can I render a child component or parallel component when a state is set?
Main component contains the following code
editValue(data)
console.log("edited")
this.setState( edits:data , function()
console.log("edited value...")
)
render()
return(
<div>
<TextComponent edit=this.editValue.bind(this)/>
<AreaComponent editedValue = this.state.edits/>
</div>
)
TextComponent:
trigger = (data) =>
this.props.edit(data)
render()
return (
<div>
<p onClick=this.trigger.bind(this,'clicked')>Click</p>
</div>
)
AreaComponent:
componentDidUpdate()
console.log("update - component")
componentWillReceiveProps(nextProps)
console.log('receive - component');
Here when I click the <p> I got my console as following
edited
receive - component
update - component
edited value...
What my issue is the edited value that I set in state is not reflecting in my AreaComponent. All the componentDidUpdate() and componentWillReceiveProps() are triggered before setting the state. I want to get the state value inside my next component. What changes should I do in this code?
reactjs
The child liofecycle method are not triggered before state is set, instead the callback to setState is triggered after the child lifecycle method are called. Also how are you checking that the state has not updated and and how are you passing data to the editValue componet
– Shubham Khatri
Mar 28 at 6:10
The state is updated but only after the re-rendering of the components. I got this by checking the console values
– Hareesh
Mar 28 at 6:20
add a comment |
How can I render a child component or parallel component when a state is set?
Main component contains the following code
editValue(data)
console.log("edited")
this.setState( edits:data , function()
console.log("edited value...")
)
render()
return(
<div>
<TextComponent edit=this.editValue.bind(this)/>
<AreaComponent editedValue = this.state.edits/>
</div>
)
TextComponent:
trigger = (data) =>
this.props.edit(data)
render()
return (
<div>
<p onClick=this.trigger.bind(this,'clicked')>Click</p>
</div>
)
AreaComponent:
componentDidUpdate()
console.log("update - component")
componentWillReceiveProps(nextProps)
console.log('receive - component');
Here when I click the <p> I got my console as following
edited
receive - component
update - component
edited value...
What my issue is the edited value that I set in state is not reflecting in my AreaComponent. All the componentDidUpdate() and componentWillReceiveProps() are triggered before setting the state. I want to get the state value inside my next component. What changes should I do in this code?
reactjs
How can I render a child component or parallel component when a state is set?
Main component contains the following code
editValue(data)
console.log("edited")
this.setState( edits:data , function()
console.log("edited value...")
)
render()
return(
<div>
<TextComponent edit=this.editValue.bind(this)/>
<AreaComponent editedValue = this.state.edits/>
</div>
)
TextComponent:
trigger = (data) =>
this.props.edit(data)
render()
return (
<div>
<p onClick=this.trigger.bind(this,'clicked')>Click</p>
</div>
)
AreaComponent:
componentDidUpdate()
console.log("update - component")
componentWillReceiveProps(nextProps)
console.log('receive - component');
Here when I click the <p> I got my console as following
edited
receive - component
update - component
edited value...
What my issue is the edited value that I set in state is not reflecting in my AreaComponent. All the componentDidUpdate() and componentWillReceiveProps() are triggered before setting the state. I want to get the state value inside my next component. What changes should I do in this code?
reactjs
reactjs
asked Mar 28 at 5:59
HareeshHareesh
6957 silver badges23 bronze badges
6957 silver badges23 bronze badges
The child liofecycle method are not triggered before state is set, instead the callback to setState is triggered after the child lifecycle method are called. Also how are you checking that the state has not updated and and how are you passing data to the editValue componet
– Shubham Khatri
Mar 28 at 6:10
The state is updated but only after the re-rendering of the components. I got this by checking the console values
– Hareesh
Mar 28 at 6:20
add a comment |
The child liofecycle method are not triggered before state is set, instead the callback to setState is triggered after the child lifecycle method are called. Also how are you checking that the state has not updated and and how are you passing data to the editValue componet
– Shubham Khatri
Mar 28 at 6:10
The state is updated but only after the re-rendering of the components. I got this by checking the console values
– Hareesh
Mar 28 at 6:20
The child liofecycle method are not triggered before state is set, instead the callback to setState is triggered after the child lifecycle method are called. Also how are you checking that the state has not updated and and how are you passing data to the editValue componet
– Shubham Khatri
Mar 28 at 6:10
The child liofecycle method are not triggered before state is set, instead the callback to setState is triggered after the child lifecycle method are called. Also how are you checking that the state has not updated and and how are you passing data to the editValue componet
– Shubham Khatri
Mar 28 at 6:10
The state is updated but only after the re-rendering of the components. I got this by checking the console values
– Hareesh
Mar 28 at 6:20
The state is updated but only after the re-rendering of the components. I got this by checking the console values
– Hareesh
Mar 28 at 6:20
add a comment |
2 Answers
2
active
oldest
votes
You seem to be misInterpreting the console results, you have looged edited value.. in the setState callback which is triggered after the component has updated due to a state change.
If you actually look into the componentWillReceiveProps and componentDidUpdate of childComponent and log the props in these lifecycle or as a matter of fact in render method you could actually see the updated value that you set in state.
You should think of setState callback as a lifecycle which is triggered after component updates similar to componentDidUpdate and componentDidUpdate of parent is triggered after componentDidUpdate of child components most often
add a comment |
Hey I tested out your code and it functions as you expected.
Here's a sandbox for you: https://codesandbox.io/s/7yry1onrq6
In order for you to see the changes after you click TextComponent just use the editedValue prop in the AreaComponent.
This is an example mirroring your code:
class AreaComponent extends React.Component
componentDidUpdate()
console.log("update - component");
componentWillReceiveProps(nextProps)
console.log("receive - component");
render()
return <p>this.props.editedValue</p>;
In the above example the editedValue is correctly changing after clicking.
Yes this works. Thanks for your suggestions
– Hareesh
Mar 28 at 6:53
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/4.0/"u003ecc by-sa 4.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55391028%2fhow-can-i-render-a-child-component-or-parallel-component-when-a-state-is-set-in%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
You seem to be misInterpreting the console results, you have looged edited value.. in the setState callback which is triggered after the component has updated due to a state change.
If you actually look into the componentWillReceiveProps and componentDidUpdate of childComponent and log the props in these lifecycle or as a matter of fact in render method you could actually see the updated value that you set in state.
You should think of setState callback as a lifecycle which is triggered after component updates similar to componentDidUpdate and componentDidUpdate of parent is triggered after componentDidUpdate of child components most often
add a comment |
You seem to be misInterpreting the console results, you have looged edited value.. in the setState callback which is triggered after the component has updated due to a state change.
If you actually look into the componentWillReceiveProps and componentDidUpdate of childComponent and log the props in these lifecycle or as a matter of fact in render method you could actually see the updated value that you set in state.
You should think of setState callback as a lifecycle which is triggered after component updates similar to componentDidUpdate and componentDidUpdate of parent is triggered after componentDidUpdate of child components most often
add a comment |
You seem to be misInterpreting the console results, you have looged edited value.. in the setState callback which is triggered after the component has updated due to a state change.
If you actually look into the componentWillReceiveProps and componentDidUpdate of childComponent and log the props in these lifecycle or as a matter of fact in render method you could actually see the updated value that you set in state.
You should think of setState callback as a lifecycle which is triggered after component updates similar to componentDidUpdate and componentDidUpdate of parent is triggered after componentDidUpdate of child components most often
You seem to be misInterpreting the console results, you have looged edited value.. in the setState callback which is triggered after the component has updated due to a state change.
If you actually look into the componentWillReceiveProps and componentDidUpdate of childComponent and log the props in these lifecycle or as a matter of fact in render method you could actually see the updated value that you set in state.
You should think of setState callback as a lifecycle which is triggered after component updates similar to componentDidUpdate and componentDidUpdate of parent is triggered after componentDidUpdate of child components most often
answered Mar 28 at 7:01
Shubham KhatriShubham Khatri
117k23 gold badges165 silver badges202 bronze badges
117k23 gold badges165 silver badges202 bronze badges
add a comment |
add a comment |
Hey I tested out your code and it functions as you expected.
Here's a sandbox for you: https://codesandbox.io/s/7yry1onrq6
In order for you to see the changes after you click TextComponent just use the editedValue prop in the AreaComponent.
This is an example mirroring your code:
class AreaComponent extends React.Component
componentDidUpdate()
console.log("update - component");
componentWillReceiveProps(nextProps)
console.log("receive - component");
render()
return <p>this.props.editedValue</p>;
In the above example the editedValue is correctly changing after clicking.
Yes this works. Thanks for your suggestions
– Hareesh
Mar 28 at 6:53
add a comment |
Hey I tested out your code and it functions as you expected.
Here's a sandbox for you: https://codesandbox.io/s/7yry1onrq6
In order for you to see the changes after you click TextComponent just use the editedValue prop in the AreaComponent.
This is an example mirroring your code:
class AreaComponent extends React.Component
componentDidUpdate()
console.log("update - component");
componentWillReceiveProps(nextProps)
console.log("receive - component");
render()
return <p>this.props.editedValue</p>;
In the above example the editedValue is correctly changing after clicking.
Yes this works. Thanks for your suggestions
– Hareesh
Mar 28 at 6:53
add a comment |
Hey I tested out your code and it functions as you expected.
Here's a sandbox for you: https://codesandbox.io/s/7yry1onrq6
In order for you to see the changes after you click TextComponent just use the editedValue prop in the AreaComponent.
This is an example mirroring your code:
class AreaComponent extends React.Component
componentDidUpdate()
console.log("update - component");
componentWillReceiveProps(nextProps)
console.log("receive - component");
render()
return <p>this.props.editedValue</p>;
In the above example the editedValue is correctly changing after clicking.
Hey I tested out your code and it functions as you expected.
Here's a sandbox for you: https://codesandbox.io/s/7yry1onrq6
In order for you to see the changes after you click TextComponent just use the editedValue prop in the AreaComponent.
This is an example mirroring your code:
class AreaComponent extends React.Component
componentDidUpdate()
console.log("update - component");
componentWillReceiveProps(nextProps)
console.log("receive - component");
render()
return <p>this.props.editedValue</p>;
In the above example the editedValue is correctly changing after clicking.
answered Mar 28 at 6:20
JaaqoJaaqo
565 bronze badges
565 bronze badges
Yes this works. Thanks for your suggestions
– Hareesh
Mar 28 at 6:53
add a comment |
Yes this works. Thanks for your suggestions
– Hareesh
Mar 28 at 6:53
Yes this works. Thanks for your suggestions
– Hareesh
Mar 28 at 6:53
Yes this works. Thanks for your suggestions
– Hareesh
Mar 28 at 6:53
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55391028%2fhow-can-i-render-a-child-component-or-parallel-component-when-a-state-is-set-in%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
The child liofecycle method are not triggered before state is set, instead the callback to setState is triggered after the child lifecycle method are called. Also how are you checking that the state has not updated and and how are you passing data to the editValue componet
– Shubham Khatri
Mar 28 at 6:10
The state is updated but only after the re-rendering of the components. I got this by checking the console values
– Hareesh
Mar 28 at 6:20