React setState re-render
up vote
0
down vote
favorite
First of all, I'm really new into React, so forgive my lack of knowledge about the subject.
As far as I know, when you setState a new value, it renders again the view (or parts of it that needs re-render).
I've got something like this, and I would like to know if it's a good practice or not, how could I solve this kind of issues to improve, etc.
class MyComponent extends Component {
constructor(props) {
super(props)
this.state = {
key: value
}
this.functionRender = this.functionRender.bind(this)
this.changeValue = this.changeValue.bind(this)
}
functionRender = () => {
if(someParams !== null) {
return <AnotherComponent param={this.state.key} />
}
else {
return "<span>Loading</span>"
}
}
changeValue = (newValue) => {
this.setState({
key: newValue
})
}
render() {
return (<div>... {this.functionRender()} ... <span onClick={() => this.changeValue(otherValue)}>Click me</span></div>)
}
}
Another component
class AnotherComponent extends Component {
constructor (props) {
super(props)
}
render () {
return (
if (this.props.param === someOptions) {
return <div>Options 1</div>
} else {
return <div>Options 2</div>
}
)
}
}
The intention of the code is that when I click on the span it will change the key of the state, and then the component <AnotherComponent />
should change because of its parameter.
I assured that when I make the setState, on the callback I throw a console log with the new value, and it's setted correctly, but the AnotherComponent
doesn't updates, because depending on the param given it shows one thing or another.
Maybe I need to use some lifecycle of the MyComponent?
Edit
I found that the param
that AnotherComponent
is receiving it does not changes, it's always the same one.
javascript reactjs
|
show 6 more comments
up vote
0
down vote
favorite
First of all, I'm really new into React, so forgive my lack of knowledge about the subject.
As far as I know, when you setState a new value, it renders again the view (or parts of it that needs re-render).
I've got something like this, and I would like to know if it's a good practice or not, how could I solve this kind of issues to improve, etc.
class MyComponent extends Component {
constructor(props) {
super(props)
this.state = {
key: value
}
this.functionRender = this.functionRender.bind(this)
this.changeValue = this.changeValue.bind(this)
}
functionRender = () => {
if(someParams !== null) {
return <AnotherComponent param={this.state.key} />
}
else {
return "<span>Loading</span>"
}
}
changeValue = (newValue) => {
this.setState({
key: newValue
})
}
render() {
return (<div>... {this.functionRender()} ... <span onClick={() => this.changeValue(otherValue)}>Click me</span></div>)
}
}
Another component
class AnotherComponent extends Component {
constructor (props) {
super(props)
}
render () {
return (
if (this.props.param === someOptions) {
return <div>Options 1</div>
} else {
return <div>Options 2</div>
}
)
}
}
The intention of the code is that when I click on the span it will change the key of the state, and then the component <AnotherComponent />
should change because of its parameter.
I assured that when I make the setState, on the callback I throw a console log with the new value, and it's setted correctly, but the AnotherComponent
doesn't updates, because depending on the param given it shows one thing or another.
Maybe I need to use some lifecycle of the MyComponent?
Edit
I found that the param
that AnotherComponent
is receiving it does not changes, it's always the same one.
javascript reactjs
2
I might be misreading this, but shouldn't this bethis.functionRender()
?
– OliverRadini
Nov 8 at 11:21
2
you don't need tobind
the functions in the constructor if you're using arrow functions
– Yoav
Nov 8 at 11:23
Totally true, I didn't copied it well! but question still remains!
– Cheshire
Nov 8 at 11:23
thanks for the advice @Yoav didn't know that!
– Cheshire
Nov 8 at 11:23
2
it would be helpful if you'll show the<AnotherComponent/>
implementation
– Yoav
Nov 8 at 11:25
|
show 6 more comments
up vote
0
down vote
favorite
up vote
0
down vote
favorite
First of all, I'm really new into React, so forgive my lack of knowledge about the subject.
As far as I know, when you setState a new value, it renders again the view (or parts of it that needs re-render).
I've got something like this, and I would like to know if it's a good practice or not, how could I solve this kind of issues to improve, etc.
class MyComponent extends Component {
constructor(props) {
super(props)
this.state = {
key: value
}
this.functionRender = this.functionRender.bind(this)
this.changeValue = this.changeValue.bind(this)
}
functionRender = () => {
if(someParams !== null) {
return <AnotherComponent param={this.state.key} />
}
else {
return "<span>Loading</span>"
}
}
changeValue = (newValue) => {
this.setState({
key: newValue
})
}
render() {
return (<div>... {this.functionRender()} ... <span onClick={() => this.changeValue(otherValue)}>Click me</span></div>)
}
}
Another component
class AnotherComponent extends Component {
constructor (props) {
super(props)
}
render () {
return (
if (this.props.param === someOptions) {
return <div>Options 1</div>
} else {
return <div>Options 2</div>
}
)
}
}
The intention of the code is that when I click on the span it will change the key of the state, and then the component <AnotherComponent />
should change because of its parameter.
I assured that when I make the setState, on the callback I throw a console log with the new value, and it's setted correctly, but the AnotherComponent
doesn't updates, because depending on the param given it shows one thing or another.
Maybe I need to use some lifecycle of the MyComponent?
Edit
I found that the param
that AnotherComponent
is receiving it does not changes, it's always the same one.
javascript reactjs
First of all, I'm really new into React, so forgive my lack of knowledge about the subject.
As far as I know, when you setState a new value, it renders again the view (or parts of it that needs re-render).
I've got something like this, and I would like to know if it's a good practice or not, how could I solve this kind of issues to improve, etc.
class MyComponent extends Component {
constructor(props) {
super(props)
this.state = {
key: value
}
this.functionRender = this.functionRender.bind(this)
this.changeValue = this.changeValue.bind(this)
}
functionRender = () => {
if(someParams !== null) {
return <AnotherComponent param={this.state.key} />
}
else {
return "<span>Loading</span>"
}
}
changeValue = (newValue) => {
this.setState({
key: newValue
})
}
render() {
return (<div>... {this.functionRender()} ... <span onClick={() => this.changeValue(otherValue)}>Click me</span></div>)
}
}
Another component
class AnotherComponent extends Component {
constructor (props) {
super(props)
}
render () {
return (
if (this.props.param === someOptions) {
return <div>Options 1</div>
} else {
return <div>Options 2</div>
}
)
}
}
The intention of the code is that when I click on the span it will change the key of the state, and then the component <AnotherComponent />
should change because of its parameter.
I assured that when I make the setState, on the callback I throw a console log with the new value, and it's setted correctly, but the AnotherComponent
doesn't updates, because depending on the param given it shows one thing or another.
Maybe I need to use some lifecycle of the MyComponent?
Edit
I found that the param
that AnotherComponent
is receiving it does not changes, it's always the same one.
javascript reactjs
javascript reactjs
edited Nov 8 at 12:15
asked Nov 8 at 11:18
Cheshire
706519
706519
2
I might be misreading this, but shouldn't this bethis.functionRender()
?
– OliverRadini
Nov 8 at 11:21
2
you don't need tobind
the functions in the constructor if you're using arrow functions
– Yoav
Nov 8 at 11:23
Totally true, I didn't copied it well! but question still remains!
– Cheshire
Nov 8 at 11:23
thanks for the advice @Yoav didn't know that!
– Cheshire
Nov 8 at 11:23
2
it would be helpful if you'll show the<AnotherComponent/>
implementation
– Yoav
Nov 8 at 11:25
|
show 6 more comments
2
I might be misreading this, but shouldn't this bethis.functionRender()
?
– OliverRadini
Nov 8 at 11:21
2
you don't need tobind
the functions in the constructor if you're using arrow functions
– Yoav
Nov 8 at 11:23
Totally true, I didn't copied it well! but question still remains!
– Cheshire
Nov 8 at 11:23
thanks for the advice @Yoav didn't know that!
– Cheshire
Nov 8 at 11:23
2
it would be helpful if you'll show the<AnotherComponent/>
implementation
– Yoav
Nov 8 at 11:25
2
2
I might be misreading this, but shouldn't this be
this.functionRender()
?– OliverRadini
Nov 8 at 11:21
I might be misreading this, but shouldn't this be
this.functionRender()
?– OliverRadini
Nov 8 at 11:21
2
2
you don't need to
bind
the functions in the constructor if you're using arrow functions– Yoav
Nov 8 at 11:23
you don't need to
bind
the functions in the constructor if you're using arrow functions– Yoav
Nov 8 at 11:23
Totally true, I didn't copied it well! but question still remains!
– Cheshire
Nov 8 at 11:23
Totally true, I didn't copied it well! but question still remains!
– Cheshire
Nov 8 at 11:23
thanks for the advice @Yoav didn't know that!
– Cheshire
Nov 8 at 11:23
thanks for the advice @Yoav didn't know that!
– Cheshire
Nov 8 at 11:23
2
2
it would be helpful if you'll show the
<AnotherComponent/>
implementation– Yoav
Nov 8 at 11:25
it would be helpful if you'll show the
<AnotherComponent/>
implementation– Yoav
Nov 8 at 11:25
|
show 6 more comments
2 Answers
2
active
oldest
votes
up vote
1
down vote
accepted
I would suggest that you'll first test it in the parent using a simple console.log
on your changeValue
function:
changeValue = (newValue) => {
console.log('newValue before', newValue);
this.setState({
key: newValue
}, ()=> console.log('newValue after', this.state.key))
}
setState
can accept a callback that will be invoked after the state actually changed (remember that setState
is async).
Since we can't see the entire component it's hard to understand what actually goes on there.
I suspect that the newValue
parameter is always the same but i can't be sure.
It seems like you're missing the props
in AnotherComponent
's constructor. it should be:
constructor (props) {
super(props) // here
}
Try replacing the if statement with:
{this.props.param === someOptions? <div>Options 1</div>: <div>Options 2</div>}
also add this function to see if the new props actually get to the component:
componentWillReceiveProps(newProps){
console.log(newProps);
}
and check for the type of param
and someOptions
since you're (rightfully) using the ===
comparison.
I tried that, and it changes the value of the state with thenewValue
. What I have is a tab selection with 3 options, and some "movements" on someones account. I can retrieve with that exact same function the tab value, but it won't change on the parameter to give it to the other component and render different movements. I really appreciate your time trying to help!
– Cheshire
Nov 8 at 12:10
In my actual code I got props in both, constructor and super, I updated the code
– Cheshire
Nov 8 at 12:15
Thanks! The componentWillReceiveProps did work. Without it, the state was not changing, while the props was. I had to update the state with it. Thanks!
– Cheshire
Nov 8 at 12:23
add a comment |
up vote
0
down vote
First, fat arrow ( =>
) autobind methods so you do not need to bind it in the constructor, second re-renders occur if you change the key of the component.
Ref: https://reactjs.org/docs/lists-and-keys.html#keys
What I've found, in the AnotherComponent, the param it's always the same, it's not changing
– Cheshire
Nov 8 at 11:57
2
It doesn't autobind, it runs in the current lexical scope.
– Davin Tryon
Nov 8 at 12:07
@DavinTryon you are correct, fat arrows don't create their own this reference but keep the reference of the context. "Autobind" was intended as "does not need to be bound"
– Mosè Raguzzini
Nov 8 at 13:18
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
accepted
I would suggest that you'll first test it in the parent using a simple console.log
on your changeValue
function:
changeValue = (newValue) => {
console.log('newValue before', newValue);
this.setState({
key: newValue
}, ()=> console.log('newValue after', this.state.key))
}
setState
can accept a callback that will be invoked after the state actually changed (remember that setState
is async).
Since we can't see the entire component it's hard to understand what actually goes on there.
I suspect that the newValue
parameter is always the same but i can't be sure.
It seems like you're missing the props
in AnotherComponent
's constructor. it should be:
constructor (props) {
super(props) // here
}
Try replacing the if statement with:
{this.props.param === someOptions? <div>Options 1</div>: <div>Options 2</div>}
also add this function to see if the new props actually get to the component:
componentWillReceiveProps(newProps){
console.log(newProps);
}
and check for the type of param
and someOptions
since you're (rightfully) using the ===
comparison.
I tried that, and it changes the value of the state with thenewValue
. What I have is a tab selection with 3 options, and some "movements" on someones account. I can retrieve with that exact same function the tab value, but it won't change on the parameter to give it to the other component and render different movements. I really appreciate your time trying to help!
– Cheshire
Nov 8 at 12:10
In my actual code I got props in both, constructor and super, I updated the code
– Cheshire
Nov 8 at 12:15
Thanks! The componentWillReceiveProps did work. Without it, the state was not changing, while the props was. I had to update the state with it. Thanks!
– Cheshire
Nov 8 at 12:23
add a comment |
up vote
1
down vote
accepted
I would suggest that you'll first test it in the parent using a simple console.log
on your changeValue
function:
changeValue = (newValue) => {
console.log('newValue before', newValue);
this.setState({
key: newValue
}, ()=> console.log('newValue after', this.state.key))
}
setState
can accept a callback that will be invoked after the state actually changed (remember that setState
is async).
Since we can't see the entire component it's hard to understand what actually goes on there.
I suspect that the newValue
parameter is always the same but i can't be sure.
It seems like you're missing the props
in AnotherComponent
's constructor. it should be:
constructor (props) {
super(props) // here
}
Try replacing the if statement with:
{this.props.param === someOptions? <div>Options 1</div>: <div>Options 2</div>}
also add this function to see if the new props actually get to the component:
componentWillReceiveProps(newProps){
console.log(newProps);
}
and check for the type of param
and someOptions
since you're (rightfully) using the ===
comparison.
I tried that, and it changes the value of the state with thenewValue
. What I have is a tab selection with 3 options, and some "movements" on someones account. I can retrieve with that exact same function the tab value, but it won't change on the parameter to give it to the other component and render different movements. I really appreciate your time trying to help!
– Cheshire
Nov 8 at 12:10
In my actual code I got props in both, constructor and super, I updated the code
– Cheshire
Nov 8 at 12:15
Thanks! The componentWillReceiveProps did work. Without it, the state was not changing, while the props was. I had to update the state with it. Thanks!
– Cheshire
Nov 8 at 12:23
add a comment |
up vote
1
down vote
accepted
up vote
1
down vote
accepted
I would suggest that you'll first test it in the parent using a simple console.log
on your changeValue
function:
changeValue = (newValue) => {
console.log('newValue before', newValue);
this.setState({
key: newValue
}, ()=> console.log('newValue after', this.state.key))
}
setState
can accept a callback that will be invoked after the state actually changed (remember that setState
is async).
Since we can't see the entire component it's hard to understand what actually goes on there.
I suspect that the newValue
parameter is always the same but i can't be sure.
It seems like you're missing the props
in AnotherComponent
's constructor. it should be:
constructor (props) {
super(props) // here
}
Try replacing the if statement with:
{this.props.param === someOptions? <div>Options 1</div>: <div>Options 2</div>}
also add this function to see if the new props actually get to the component:
componentWillReceiveProps(newProps){
console.log(newProps);
}
and check for the type of param
and someOptions
since you're (rightfully) using the ===
comparison.
I would suggest that you'll first test it in the parent using a simple console.log
on your changeValue
function:
changeValue = (newValue) => {
console.log('newValue before', newValue);
this.setState({
key: newValue
}, ()=> console.log('newValue after', this.state.key))
}
setState
can accept a callback that will be invoked after the state actually changed (remember that setState
is async).
Since we can't see the entire component it's hard to understand what actually goes on there.
I suspect that the newValue
parameter is always the same but i can't be sure.
It seems like you're missing the props
in AnotherComponent
's constructor. it should be:
constructor (props) {
super(props) // here
}
Try replacing the if statement with:
{this.props.param === someOptions? <div>Options 1</div>: <div>Options 2</div>}
also add this function to see if the new props actually get to the component:
componentWillReceiveProps(newProps){
console.log(newProps);
}
and check for the type of param
and someOptions
since you're (rightfully) using the ===
comparison.
edited Nov 8 at 12:19
answered Nov 8 at 12:06
Yoav
2,06632055
2,06632055
I tried that, and it changes the value of the state with thenewValue
. What I have is a tab selection with 3 options, and some "movements" on someones account. I can retrieve with that exact same function the tab value, but it won't change on the parameter to give it to the other component and render different movements. I really appreciate your time trying to help!
– Cheshire
Nov 8 at 12:10
In my actual code I got props in both, constructor and super, I updated the code
– Cheshire
Nov 8 at 12:15
Thanks! The componentWillReceiveProps did work. Without it, the state was not changing, while the props was. I had to update the state with it. Thanks!
– Cheshire
Nov 8 at 12:23
add a comment |
I tried that, and it changes the value of the state with thenewValue
. What I have is a tab selection with 3 options, and some "movements" on someones account. I can retrieve with that exact same function the tab value, but it won't change on the parameter to give it to the other component and render different movements. I really appreciate your time trying to help!
– Cheshire
Nov 8 at 12:10
In my actual code I got props in both, constructor and super, I updated the code
– Cheshire
Nov 8 at 12:15
Thanks! The componentWillReceiveProps did work. Without it, the state was not changing, while the props was. I had to update the state with it. Thanks!
– Cheshire
Nov 8 at 12:23
I tried that, and it changes the value of the state with the
newValue
. What I have is a tab selection with 3 options, and some "movements" on someones account. I can retrieve with that exact same function the tab value, but it won't change on the parameter to give it to the other component and render different movements. I really appreciate your time trying to help!– Cheshire
Nov 8 at 12:10
I tried that, and it changes the value of the state with the
newValue
. What I have is a tab selection with 3 options, and some "movements" on someones account. I can retrieve with that exact same function the tab value, but it won't change on the parameter to give it to the other component and render different movements. I really appreciate your time trying to help!– Cheshire
Nov 8 at 12:10
In my actual code I got props in both, constructor and super, I updated the code
– Cheshire
Nov 8 at 12:15
In my actual code I got props in both, constructor and super, I updated the code
– Cheshire
Nov 8 at 12:15
Thanks! The componentWillReceiveProps did work. Without it, the state was not changing, while the props was. I had to update the state with it. Thanks!
– Cheshire
Nov 8 at 12:23
Thanks! The componentWillReceiveProps did work. Without it, the state was not changing, while the props was. I had to update the state with it. Thanks!
– Cheshire
Nov 8 at 12:23
add a comment |
up vote
0
down vote
First, fat arrow ( =>
) autobind methods so you do not need to bind it in the constructor, second re-renders occur if you change the key of the component.
Ref: https://reactjs.org/docs/lists-and-keys.html#keys
What I've found, in the AnotherComponent, the param it's always the same, it's not changing
– Cheshire
Nov 8 at 11:57
2
It doesn't autobind, it runs in the current lexical scope.
– Davin Tryon
Nov 8 at 12:07
@DavinTryon you are correct, fat arrows don't create their own this reference but keep the reference of the context. "Autobind" was intended as "does not need to be bound"
– Mosè Raguzzini
Nov 8 at 13:18
add a comment |
up vote
0
down vote
First, fat arrow ( =>
) autobind methods so you do not need to bind it in the constructor, second re-renders occur if you change the key of the component.
Ref: https://reactjs.org/docs/lists-and-keys.html#keys
What I've found, in the AnotherComponent, the param it's always the same, it's not changing
– Cheshire
Nov 8 at 11:57
2
It doesn't autobind, it runs in the current lexical scope.
– Davin Tryon
Nov 8 at 12:07
@DavinTryon you are correct, fat arrows don't create their own this reference but keep the reference of the context. "Autobind" was intended as "does not need to be bound"
– Mosè Raguzzini
Nov 8 at 13:18
add a comment |
up vote
0
down vote
up vote
0
down vote
First, fat arrow ( =>
) autobind methods so you do not need to bind it in the constructor, second re-renders occur if you change the key of the component.
Ref: https://reactjs.org/docs/lists-and-keys.html#keys
First, fat arrow ( =>
) autobind methods so you do not need to bind it in the constructor, second re-renders occur if you change the key of the component.
Ref: https://reactjs.org/docs/lists-and-keys.html#keys
answered Nov 8 at 11:44
Mosè Raguzzini
2,5351018
2,5351018
What I've found, in the AnotherComponent, the param it's always the same, it's not changing
– Cheshire
Nov 8 at 11:57
2
It doesn't autobind, it runs in the current lexical scope.
– Davin Tryon
Nov 8 at 12:07
@DavinTryon you are correct, fat arrows don't create their own this reference but keep the reference of the context. "Autobind" was intended as "does not need to be bound"
– Mosè Raguzzini
Nov 8 at 13:18
add a comment |
What I've found, in the AnotherComponent, the param it's always the same, it's not changing
– Cheshire
Nov 8 at 11:57
2
It doesn't autobind, it runs in the current lexical scope.
– Davin Tryon
Nov 8 at 12:07
@DavinTryon you are correct, fat arrows don't create their own this reference but keep the reference of the context. "Autobind" was intended as "does not need to be bound"
– Mosè Raguzzini
Nov 8 at 13:18
What I've found, in the AnotherComponent, the param it's always the same, it's not changing
– Cheshire
Nov 8 at 11:57
What I've found, in the AnotherComponent, the param it's always the same, it's not changing
– Cheshire
Nov 8 at 11:57
2
2
It doesn't autobind, it runs in the current lexical scope.
– Davin Tryon
Nov 8 at 12:07
It doesn't autobind, it runs in the current lexical scope.
– Davin Tryon
Nov 8 at 12:07
@DavinTryon you are correct, fat arrows don't create their own this reference but keep the reference of the context. "Autobind" was intended as "does not need to be bound"
– Mosè Raguzzini
Nov 8 at 13:18
@DavinTryon you are correct, fat arrows don't create their own this reference but keep the reference of the context. "Autobind" was intended as "does not need to be bound"
– Mosè Raguzzini
Nov 8 at 13:18
add a comment |
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%2f53206695%2freact-setstate-re-render%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
2
I might be misreading this, but shouldn't this be
this.functionRender()
?– OliverRadini
Nov 8 at 11:21
2
you don't need to
bind
the functions in the constructor if you're using arrow functions– Yoav
Nov 8 at 11:23
Totally true, I didn't copied it well! but question still remains!
– Cheshire
Nov 8 at 11:23
thanks for the advice @Yoav didn't know that!
– Cheshire
Nov 8 at 11:23
2
it would be helpful if you'll show the
<AnotherComponent/>
implementation– Yoav
Nov 8 at 11:25