Changing defaultValue doesn't re-render input, causes hidden state in the db
See original GitHub issueI posted this on StackOverflow but the dearth of answers makes me think it might be a bug.
Given the following simple code
const {createFactory, createClass, DOM: { label, input, button }} = React;
const tester = createFactory(createClass({
render() {
return label({}
,`Name: ${this.props.name}`
,input({defaultValue: this.props.name})
,button({onClick: this.changeName}, "Change")
)
},
changeName() {
this.setProps({name: "Wilma"})
}
}) )
React.render(tester({name: "Fred"}), document.querySelector('body'))
clicking the button doesn’t re-render the input and it still says “Fred” even though this.props.name
is “Wilma”.
Issue Analytics
- State:
- Created 8 years ago
- Comments:8
Top Results From Across the Web
defaultValue change does not re-render input - Stack Overflow
I found what seems to be a pretty good solution to this: Use the key prop to force rendering of an entirely new...
Read more >Re-render DefaultValue when Value Changes in React
React will never update the input tag if the value passed to defaultValue changes. It was meant to be a default/starting value only,...
Read more >How to Save State to LocalStorage & Persist on Refresh with ...
We'll set that preference in localStorage and read that value so that any time they ... Step 1: Using React state to hide...
Read more >React Gotchas, Anti-Patterns, and Best Practices | by Steven Li
A potential anti-pattern is calling setState in this method as it would cause an additional re-render. If you find yourself setting the state...
Read more >Dynamic behavior in Svelte: working with variables and props
You can also specify a default initial value for a prop. This will be used if the component's consumer doesn't specify the prop...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
I posted this on the SO but I’ll include it here as well.
I found what seems to be a pretty good solution to this: Use the
key
prop to force rendering of an entirely newinput
.In my particular case, I don’t need the
input
to be controlled with its ownonChange
prop, as theform
surrounding it ultimately controls the state within some store which populates thedefaultValue
. But the store’s state might be asynchronously initialized/updated, and in which case thedefaultValue
should be updated. So here is a condensed version of my particular case:The answer you got on Stack Overflow is correct.
defaultValue
only specifies the value when the input is initially created; if you want it to remain updated you need to usevalue
and handleonChange
events correctly.We don’t update when
defaultValue
changes because otherwise you have two sources (the user’s input and your component props) trying to control the value, and it wouldn’t be clear what should happen when they conflict. Instead, we force you to think about and write out the actual behavior you want.