Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TextField defaultValue prop not being set on re-render #1328

Closed
coniel opened this issue Aug 3, 2015 · 12 comments
Closed

TextField defaultValue prop not being set on re-render #1328

coniel opened this issue Aug 3, 2015 · 12 comments
Labels
out of scope The problem looks valid but we won't fix it (maybe we will revisit it in the future)

Comments

@coniel
Copy link

coniel commented Aug 3, 2015

If I set the defaultValue of a TextField to a state param e.g:

<TextField defaultValue={this.state.defaultValue} ... />

It doesn't set the defaultValue prop when re-rendering due to a state change. If I set an initial value in getIntialState then it shows the initial value, but does not update when re-dendering. The floating label however does go into the active position (as if it had a value).

@KaiHufenbach
Copy link

This is standard behaviour: It is only useful for setting value initially. =)
Please refer controlled vs. uncontrolled components: https://facebook.github.io/react/docs/forms.html

So you should use the following pattern:

onChangeFunction(component, value){
    this.setValue({value:value});
}

<... value = {this.state.value} onChange={this.onChangeFunction} .../>

When you are loading content asynchronously you either have to keep track of the value on your own (controlled component) or attach the component after the content is loaded (by using the state to check whether loading is complete).

@coniel
Copy link
Author

coniel commented Aug 3, 2015

Ah, I had a feeling I was doing something wrong (I'm new to React). Thanks for the help!

@coniel coniel closed this as completed Aug 3, 2015
@codeVerine
Copy link

Will this work now? We will not get component and value in the handler, right ? And the this is not bound too.

@oliviertassinari oliviertassinari added the out of scope The problem looks valid but we won't fix it (maybe we will revisit it in the future) label Feb 18, 2016
@oliviertassinari
Copy link
Member

Will this work now?

@codeVerine No it doesn't. Have a look at @KaiHufenbach comment. We won't fix it.

@arcmode
Copy link
Contributor

arcmode commented Mar 17, 2016

@codeVerine You can include your desired default value on the key prop and it will be rendered again. It's hacky but so far it works well when you can't go with controlled mode.

@ORESoftware
Copy link

ORESoftware commented Dec 13, 2016

Given this thread, wondering if anyone could solve this problem a different way? Would it possible to conditionally render either value or defaultValue?

https://stackoverflow.com/questions/41116891/conditionally-add-remove-property-from-element-with-react

Please see question on SO, thanks

@ORESoftware
Copy link

ORESoftware commented Dec 13, 2016

@drojas can you give an example of what you just mentioned? "You can include your desired default value on the key prop and it will be rendered again. It's hacky but so far it works well when you can't go with controlled mode." I don't quite follow, code would help thanks. I think I understand, but I don't know if that key prop methodology would work for other elements besides TextField would it?

@arcmode
Copy link
Contributor

arcmode commented Dec 13, 2016

Hi @ORESoftware I've been using props like these:
screen shot 2016-12-13 at 8 48 26 am
By doing this, when the defaultValue changes my react node will have a different key prop and therefore a new component will be mounted to replace the previous one. It can lead to leaking memory for sure and I've not tested that matter so my advice is to go with fully controlled mode if you still can.

@ChinW
Copy link

ChinW commented Jan 8, 2017

@codeVerine I was waiting for this answer for so long, lol, thx

@abdulhadi1995
Copy link

@drojas i was going nuts over this thank you so much.!

@abdullRahman95
Copy link

abdullRahman95 commented Sep 23, 2021

@codeVerine You can include your desired default value on the key prop and it will be rendered again. It's hacky but so far it works well when you can't go with controlled mode.

Thanks <3, its working!

@johnwaweru
Copy link

johnwaweru commented Dec 17, 2021

Setting the key re-renders the form and the input field loses focus as soon as you begin typing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
out of scope The problem looks valid but we won't fix it (maybe we will revisit it in the future)
Projects
None yet
Development

No branches or pull requests

10 participants