mirror of
https://github.com/reactjs/react.dev.git
synced 2026-02-23 20:23:08 +00:00
Add more information about defaultValue
I came across an issue on a React project where I needed to programatically set the default value of an input as the result of an async call. (I was prepopulating a zip code field from the Google Maps Geocoding API). It didn't work when I passed in an updated defaultValue prop, until I came across this StackOverflow: http://stackoverflow.com/questions/30146105/react-input-defaultvalue-doesnt-update-with-state. I was able to get it to do what I wanted by passing in the value prop instead.
This commit is contained in:
@@ -81,6 +81,8 @@ An `<input>` that does not supply a `value` (or sets it to `null`) is an *uncont
|
||||
|
||||
This will render an input that starts off with an empty value. Any user input will be immediately reflected by the rendered element. If you wanted to listen to updates to the value, you could use the `onChange` event just like you can with controlled components.
|
||||
|
||||
###Default Value
|
||||
|
||||
If you want to initialize the component with a non-empty value, you can supply a `defaultValue` prop. For example:
|
||||
|
||||
```javascript
|
||||
@@ -89,7 +91,7 @@ If you want to initialize the component with a non-empty value, you can supply a
|
||||
}
|
||||
```
|
||||
|
||||
This example will function much like the **Controlled Components** example above.
|
||||
This example will function much like the **Controlled Components** example above. *Note:* the `defaultValue` prop is only applied when the component mounts for the first time. If you need to programatically set the initial value of an input as a result of an asynchronous function call, for example, you will have to pass in that initial value with the `value` prop instead.
|
||||
|
||||
Likewise, `<input>` supports `defaultChecked` and `<select>` supports `defaultValue`.
|
||||
|
||||
|
||||
Reference in New Issue
Block a user