Controlled input changing on setState()How to create a controlled input with empty default in React 15React - changing an uncontrolled inputEditdamnpoll is changing an uncontrolled input of type text to be controlledReact form error changing a controlled input of type text to be uncontrolledReact warning: “is changing an uncontrolled input of type hidden to be controlled” why?Input elements should not switch from controlled to uncontrolled ReactJs errorReact - Change a JSON Object in setStateWarning when changing controlled input value in ReactIs this error Important? Component uncontrolled to controlledWhy I'm thrown the Warning: A component is changing an uncontrolled input of type text to be controlled
How did the Super Star Destroyer Executor get destroyed exactly?
How can saying a song's name be a copyright violation?
Why is consensus so controversial in Britain?
How can I determine if the org that I'm currently connected to is a scratch org?
Is there a hemisphere-neutral way of specifying a season?
What are some good books on Machine Learning and AI like Krugman, Wells and Graddy's "Essentials of Economics"
Expand and Contract
Could the museum Saturn V's be refitted for one more flight?
Can we compute the area of a quadrilateral with one right angle when we only know the lengths of any three sides?
Forgetting the musical notes while performing in concert
A category-like structure without composition?
Reverse dictionary where values are lists
How do I handle a potential work/personal life conflict as the manager of one of my friends?
CAST throwing error when run in stored procedure but not when run as raw query
Plagiarism or not?
What killed these X2 caps?
Can compressed videos be decoded back to their uncompresed original format?
Should I tell management that I intend to leave due to bad software development practices?
What method can I use to design a dungeon difficult enough that the PCs can't make it through without killing them?
Why do bosons tend to occupy the same state?
How to Recreate this in LaTeX? (Unsure What the Notation is Called)
How could indestructible materials be used in power generation?
Bullying boss launched a smear campaign and made me unemployable
Short story with a alien planet, government officials must wear exploding medallions
Controlled input changing on setState()
How to create a controlled input with empty default in React 15React - changing an uncontrolled inputEditdamnpoll is changing an uncontrolled input of type text to be controlledReact form error changing a controlled input of type text to be uncontrolledReact warning: “is changing an uncontrolled input of type hidden to be controlled” why?Input elements should not switch from controlled to uncontrolled ReactJs errorReact - Change a JSON Object in setStateWarning when changing controlled input value in ReactIs this error Important? Component uncontrolled to controlledWhy I'm thrown the Warning: A component is changing an uncontrolled input of type text to be controlled
I have this constructor in React component:
constructor() {
super();
this.state =
info:
title: '',
description: '',
height: ''
...
And a form with inputs controlled by the state:
<form onSubmit=this.handleFormSubmit>
<label>Title:</label>
<input type="text" name="title" value=this.state.info.title onChange=(e) => this.handleChange(e) />
<label>Description:</label>
<input type="text" name="description" value=this.state.info.description onChange=(e) => this.handleChange(e) />
...
When I type anything on the form, I guess there's something wrong with my handler, as I get the warning "Warning: A component is changing a controlled input of type text to be uncontrolled. Input elements should not switch from controlled to uncontrolled (or vice versa). Decide between using a controlled or uncontrolled input element for the lifetime of the component."
Checking console, it seems state is updating each property value that is being typed, and removing other properties, while it should remain all of them and only update the changed ones.
Here's my handler:
handleChange(event)
let name, value = event.target;
this.setState(
info:
[name]: value
);
reactjs
add a comment |
I have this constructor in React component:
constructor() {
super();
this.state =
info:
title: '',
description: '',
height: ''
...
And a form with inputs controlled by the state:
<form onSubmit=this.handleFormSubmit>
<label>Title:</label>
<input type="text" name="title" value=this.state.info.title onChange=(e) => this.handleChange(e) />
<label>Description:</label>
<input type="text" name="description" value=this.state.info.description onChange=(e) => this.handleChange(e) />
...
When I type anything on the form, I guess there's something wrong with my handler, as I get the warning "Warning: A component is changing a controlled input of type text to be uncontrolled. Input elements should not switch from controlled to uncontrolled (or vice versa). Decide between using a controlled or uncontrolled input element for the lifetime of the component."
Checking console, it seems state is updating each property value that is being typed, and removing other properties, while it should remain all of them and only update the changed ones.
Here's my handler:
handleChange(event)
let name, value = event.target;
this.setState(
info:
[name]: value
);
reactjs
add a comment |
I have this constructor in React component:
constructor() {
super();
this.state =
info:
title: '',
description: '',
height: ''
...
And a form with inputs controlled by the state:
<form onSubmit=this.handleFormSubmit>
<label>Title:</label>
<input type="text" name="title" value=this.state.info.title onChange=(e) => this.handleChange(e) />
<label>Description:</label>
<input type="text" name="description" value=this.state.info.description onChange=(e) => this.handleChange(e) />
...
When I type anything on the form, I guess there's something wrong with my handler, as I get the warning "Warning: A component is changing a controlled input of type text to be uncontrolled. Input elements should not switch from controlled to uncontrolled (or vice versa). Decide between using a controlled or uncontrolled input element for the lifetime of the component."
Checking console, it seems state is updating each property value that is being typed, and removing other properties, while it should remain all of them and only update the changed ones.
Here's my handler:
handleChange(event)
let name, value = event.target;
this.setState(
info:
[name]: value
);
reactjs
I have this constructor in React component:
constructor() {
super();
this.state =
info:
title: '',
description: '',
height: ''
...
And a form with inputs controlled by the state:
<form onSubmit=this.handleFormSubmit>
<label>Title:</label>
<input type="text" name="title" value=this.state.info.title onChange=(e) => this.handleChange(e) />
<label>Description:</label>
<input type="text" name="description" value=this.state.info.description onChange=(e) => this.handleChange(e) />
...
When I type anything on the form, I guess there's something wrong with my handler, as I get the warning "Warning: A component is changing a controlled input of type text to be uncontrolled. Input elements should not switch from controlled to uncontrolled (or vice versa). Decide between using a controlled or uncontrolled input element for the lifetime of the component."
Checking console, it seems state is updating each property value that is being typed, and removing other properties, while it should remain all of them and only update the changed ones.
Here's my handler:
handleChange(event)
let name, value = event.target;
this.setState(
info:
[name]: value
);
reactjs
reactjs
asked Mar 21 at 21:16
BiomehanikaBiomehanika
796829
796829
add a comment |
add a comment |
3 Answers
3
active
oldest
votes
the one you are using is updating only one props and the others being stripped that causes React shows warning. you can use spreading to keep the others
handleChange(event)
let name, value = event.target;
this.setState(
info:
...this.state.info,
[name]: value
);
THANK YOU. I'll tick in some minutes.
– Biomehanika
Mar 21 at 21:29
you may want to use the function version of set state in this case, so you don't end up with stale values...
– Garrett Motzner
Mar 21 at 21:30
add a comment |
There's usually 2 cases which cause such warnings to manifest in my experience.
- The initial value of the property in state is undefined and not being changed when updated
- The initial value is '' and being changed to null
add a comment |
Try this:
handleChange(event)
let name, value = event.target;
this.setState((info) =>(
info:
[name]: value
));
Breaking down the function call:
- First you have
this.setState(updaterFunction).updaterFunctiongets called bysetStatewith the previous state as the argument, and that function is expected to return an object with the keys of the state to update (it gets merged shallowly with the previous state). - Because
setStateis only a shallow merge (no matter if you pass it an object or a function), if you have an object at likethis.state.foo.barand you update the state with an object likefoo: bar: 'qux', the oldfoowill not get merged with the new foo, it will instead get replaced. So your updater function needs to do the deeper merging manually. - The updaterFunction looks like this
(info)=>(…). We pullinfoout of the previous state, and we return an object, usinginfoto manually do the deeper merging.
The benefit of passing a function to setState (instead of just an object) is that if you use the state passed to the function instead of this.state, you will avoid some potential bugs when multiple setState calls get batched together....
Thank you Garret, also great alternative
– Biomehanika
Mar 22 at 6:54
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/3.0/"u003ecc by-sa 3.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%2f55289374%2fcontrolled-input-changing-on-setstate%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
the one you are using is updating only one props and the others being stripped that causes React shows warning. you can use spreading to keep the others
handleChange(event)
let name, value = event.target;
this.setState(
info:
...this.state.info,
[name]: value
);
THANK YOU. I'll tick in some minutes.
– Biomehanika
Mar 21 at 21:29
you may want to use the function version of set state in this case, so you don't end up with stale values...
– Garrett Motzner
Mar 21 at 21:30
add a comment |
the one you are using is updating only one props and the others being stripped that causes React shows warning. you can use spreading to keep the others
handleChange(event)
let name, value = event.target;
this.setState(
info:
...this.state.info,
[name]: value
);
THANK YOU. I'll tick in some minutes.
– Biomehanika
Mar 21 at 21:29
you may want to use the function version of set state in this case, so you don't end up with stale values...
– Garrett Motzner
Mar 21 at 21:30
add a comment |
the one you are using is updating only one props and the others being stripped that causes React shows warning. you can use spreading to keep the others
handleChange(event)
let name, value = event.target;
this.setState(
info:
...this.state.info,
[name]: value
);
the one you are using is updating only one props and the others being stripped that causes React shows warning. you can use spreading to keep the others
handleChange(event)
let name, value = event.target;
this.setState(
info:
...this.state.info,
[name]: value
);
edited Mar 21 at 22:26
answered Mar 21 at 21:27
duc maiduc mai
38336
38336
THANK YOU. I'll tick in some minutes.
– Biomehanika
Mar 21 at 21:29
you may want to use the function version of set state in this case, so you don't end up with stale values...
– Garrett Motzner
Mar 21 at 21:30
add a comment |
THANK YOU. I'll tick in some minutes.
– Biomehanika
Mar 21 at 21:29
you may want to use the function version of set state in this case, so you don't end up with stale values...
– Garrett Motzner
Mar 21 at 21:30
THANK YOU. I'll tick in some minutes.
– Biomehanika
Mar 21 at 21:29
THANK YOU. I'll tick in some minutes.
– Biomehanika
Mar 21 at 21:29
you may want to use the function version of set state in this case, so you don't end up with stale values...
– Garrett Motzner
Mar 21 at 21:30
you may want to use the function version of set state in this case, so you don't end up with stale values...
– Garrett Motzner
Mar 21 at 21:30
add a comment |
There's usually 2 cases which cause such warnings to manifest in my experience.
- The initial value of the property in state is undefined and not being changed when updated
- The initial value is '' and being changed to null
add a comment |
There's usually 2 cases which cause such warnings to manifest in my experience.
- The initial value of the property in state is undefined and not being changed when updated
- The initial value is '' and being changed to null
add a comment |
There's usually 2 cases which cause such warnings to manifest in my experience.
- The initial value of the property in state is undefined and not being changed when updated
- The initial value is '' and being changed to null
There's usually 2 cases which cause such warnings to manifest in my experience.
- The initial value of the property in state is undefined and not being changed when updated
- The initial value is '' and being changed to null
answered Mar 21 at 21:26
ThatCoderGuyThatCoderGuy
42226
42226
add a comment |
add a comment |
Try this:
handleChange(event)
let name, value = event.target;
this.setState((info) =>(
info:
[name]: value
));
Breaking down the function call:
- First you have
this.setState(updaterFunction).updaterFunctiongets called bysetStatewith the previous state as the argument, and that function is expected to return an object with the keys of the state to update (it gets merged shallowly with the previous state). - Because
setStateis only a shallow merge (no matter if you pass it an object or a function), if you have an object at likethis.state.foo.barand you update the state with an object likefoo: bar: 'qux', the oldfoowill not get merged with the new foo, it will instead get replaced. So your updater function needs to do the deeper merging manually. - The updaterFunction looks like this
(info)=>(…). We pullinfoout of the previous state, and we return an object, usinginfoto manually do the deeper merging.
The benefit of passing a function to setState (instead of just an object) is that if you use the state passed to the function instead of this.state, you will avoid some potential bugs when multiple setState calls get batched together....
Thank you Garret, also great alternative
– Biomehanika
Mar 22 at 6:54
add a comment |
Try this:
handleChange(event)
let name, value = event.target;
this.setState((info) =>(
info:
[name]: value
));
Breaking down the function call:
- First you have
this.setState(updaterFunction).updaterFunctiongets called bysetStatewith the previous state as the argument, and that function is expected to return an object with the keys of the state to update (it gets merged shallowly with the previous state). - Because
setStateis only a shallow merge (no matter if you pass it an object or a function), if you have an object at likethis.state.foo.barand you update the state with an object likefoo: bar: 'qux', the oldfoowill not get merged with the new foo, it will instead get replaced. So your updater function needs to do the deeper merging manually. - The updaterFunction looks like this
(info)=>(…). We pullinfoout of the previous state, and we return an object, usinginfoto manually do the deeper merging.
The benefit of passing a function to setState (instead of just an object) is that if you use the state passed to the function instead of this.state, you will avoid some potential bugs when multiple setState calls get batched together....
Thank you Garret, also great alternative
– Biomehanika
Mar 22 at 6:54
add a comment |
Try this:
handleChange(event)
let name, value = event.target;
this.setState((info) =>(
info:
[name]: value
));
Breaking down the function call:
- First you have
this.setState(updaterFunction).updaterFunctiongets called bysetStatewith the previous state as the argument, and that function is expected to return an object with the keys of the state to update (it gets merged shallowly with the previous state). - Because
setStateis only a shallow merge (no matter if you pass it an object or a function), if you have an object at likethis.state.foo.barand you update the state with an object likefoo: bar: 'qux', the oldfoowill not get merged with the new foo, it will instead get replaced. So your updater function needs to do the deeper merging manually. - The updaterFunction looks like this
(info)=>(…). We pullinfoout of the previous state, and we return an object, usinginfoto manually do the deeper merging.
The benefit of passing a function to setState (instead of just an object) is that if you use the state passed to the function instead of this.state, you will avoid some potential bugs when multiple setState calls get batched together....
Try this:
handleChange(event)
let name, value = event.target;
this.setState((info) =>(
info:
[name]: value
));
Breaking down the function call:
- First you have
this.setState(updaterFunction).updaterFunctiongets called bysetStatewith the previous state as the argument, and that function is expected to return an object with the keys of the state to update (it gets merged shallowly with the previous state). - Because
setStateis only a shallow merge (no matter if you pass it an object or a function), if you have an object at likethis.state.foo.barand you update the state with an object likefoo: bar: 'qux', the oldfoowill not get merged with the new foo, it will instead get replaced. So your updater function needs to do the deeper merging manually. - The updaterFunction looks like this
(info)=>(…). We pullinfoout of the previous state, and we return an object, usinginfoto manually do the deeper merging.
The benefit of passing a function to setState (instead of just an object) is that if you use the state passed to the function instead of this.state, you will avoid some potential bugs when multiple setState calls get batched together....
edited Mar 21 at 21:45
answered Mar 21 at 21:33
Garrett MotznerGarrett Motzner
823315
823315
Thank you Garret, also great alternative
– Biomehanika
Mar 22 at 6:54
add a comment |
Thank you Garret, also great alternative
– Biomehanika
Mar 22 at 6:54
Thank you Garret, also great alternative
– Biomehanika
Mar 22 at 6:54
Thank you Garret, also great alternative
– Biomehanika
Mar 22 at 6:54
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%2f55289374%2fcontrolled-input-changing-on-setstate%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