How to set initialValues when data is fetched asynchronously into multiple dropdown fields in redux form ?
See original GitHub issueMy application contains a redux form which has multiple dropdowns that fetch list of items asynchronously from remote api. Now what i want to do is set the first item from each of the list of items to be set to default, so i created multiple components in my form that does the fetching but how do i set the initial values ?
For e.g. i created UserSelectorInput component for fetching the list of users remotely.
class UserSelectorInput extends Component {
constructor() {
super()
this.state = {
loading: false,
users: []
}
}
componentDidMount() {
// getAllUsers() is an action creator that fetches the list of users from remote api
this.setState({ loading: true }, () => this.props.getAllUsers())
}
componentWillReceiveProps(nextProps) {
const { allUsersList } = nextProps.users
if (allUsersList) {
const { users } = allUsersList
if (users) {
this.setState({ loading: false, users })
// set the first user item as initial value
var initialObject = {}
initialObject[this.props.name] = users[0]._id
this.props.initialize(initialObject)
}
}
}
render() {
const { users, loading } = this.state
return (
<Field
name={this.props.name}
component={DropDownWithIcon}
children={users}
loading={loading}
search={this.state.users.length > 5 && true}
onChange={(e, value) => {
if (this.props.reload) { this.props.reload(value) }
} }
label={this.props.label} />
)
}
}
const mapStateToProps = state => {
return { users: state.users }
}
export default connect(mapStateToProps)(UserSelectorInput)
Similarly, there are other components in the form which fetch values but i cannot call this.props.initialize on them because it works only on the first component. So how do i set initial values for other components ?
What’s your environment?
Versions are as follows: “redux”: “^3.5.2”, “redux-form”: “^6.3.2”
Issue Analytics
- State:
- Created 6 years ago
- Comments:11 (1 by maintainers)
Top Results From Across the Web
Initializing From State - Redux Form
Pass a enableReinitialize prop or reduxForm() config parameter set to true to allow the form the reinitialize with new "pristine" values every time...
Read more >Redux-Form initial values from - reactjs - Stack Overflow
To set the initialValues it is important to apply the reduxForm() decorator before the connect() decorator from redux. The fields will not ...
Read more >React + Redux Form — Two ways to add initial values
1) Pass initialValues prop in form instance component ... We pass a prop called initialValues into the form instance component and the value...
Read more >useForm | React Hook Form - Simple React forms validation
React hooks for form validation ... useForm is a custom hook for managing forms with ease. ... It supports both sync and async...
Read more >Form - Ant Design
Including data collection, verification, and styles. When to use. When you need to create an instance or collect information. When you need to...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop 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
Top GitHub Comments
you need to set keepDirtyOnReinitialize to true to prevent initialValues being ovewritten
http://redux-form.com/6.8.0/docs/api/ReduxForm.md/
Thanks for the response but the problem is that i have multiple components (in the same redux form) that are fetching data from api.
Now i’m setting { initialValues: … } in mapStateToProps in each of these components but the initialValues is being set only for the first component. How do i set initialValues for each of these components ? For e.g.
I can only set initialValue for any one of the component but i would like to initialize all of them.
I apologize for not being very clear in my earlier post.