以 redux 形式异步声明 initialValues 的 API 是什么?
What's the API for declaring initialValues asyncronously in redux-form?
考虑 this example 使用版本 5.3.2
:
@reduxForm({
form: 'contact',
}, (state, ownProps) => ({ // check the current component's props
initialValues: ownProps.data.loading
? '' // nothing if still loading
: ownProps.data.allPeople.people[0].name, // data if done fetching
}))
The documented demo code for version >6
未将此函数回调模式显示为一种可能性:
// Decorate with reduxForm(). It will read the initialValues prop provided by connect()
InitializeFromStateForm = reduxForm({
form: 'initializeFromState' // a unique identifier for this form
})(InitializeFromStateForm)
// You have to connect() to any reducers that you wish to connect to yourself
InitializeFromStateForm = connect(
state => ({
initialValues: state.account.data // pull initial values from account reducer
}),
{ load: loadAccount } // bind account loading action creator
)(InitializeFromStateForm)
第一种模式还有可能吗?如果是这样,它是如何工作的?它记录在任何地方吗?我在我的组件道具中看到 I'm given an initialize
dispatcher。是这样吗?
我会给你一个 apollo 的具体答案,虽然你在你的问题中根本没有提到 apollo,只是在标签中。看来你是故意的。
当你用 apollo 包装你的组件时,将 prop 名称重新映射到 initialValues 下,以便 redux-form 可以选择它,例如:
import MyComponent from './MyComponent'
import { gql, graphql } from 'react-apollo'
const currentUser = gql`
query viewer {
viewer {
firstName
lastName
}
}
`
export default graphql(currentUser, {
props: ({ ownProps, data: { loading, viewer } }) => ({
loading,
initialValues: {
firstName: viewer && viewer.firstName,
lastName: viewer && viewer.lastName,
},
})
)(MyComponent)
考虑 this example 使用版本 5.3.2
:
@reduxForm({
form: 'contact',
}, (state, ownProps) => ({ // check the current component's props
initialValues: ownProps.data.loading
? '' // nothing if still loading
: ownProps.data.allPeople.people[0].name, // data if done fetching
}))
The documented demo code for version >6
未将此函数回调模式显示为一种可能性:
// Decorate with reduxForm(). It will read the initialValues prop provided by connect()
InitializeFromStateForm = reduxForm({
form: 'initializeFromState' // a unique identifier for this form
})(InitializeFromStateForm)
// You have to connect() to any reducers that you wish to connect to yourself
InitializeFromStateForm = connect(
state => ({
initialValues: state.account.data // pull initial values from account reducer
}),
{ load: loadAccount } // bind account loading action creator
)(InitializeFromStateForm)
第一种模式还有可能吗?如果是这样,它是如何工作的?它记录在任何地方吗?我在我的组件道具中看到 I'm given an initialize
dispatcher。是这样吗?
我会给你一个 apollo 的具体答案,虽然你在你的问题中根本没有提到 apollo,只是在标签中。看来你是故意的。
当你用 apollo 包装你的组件时,将 prop 名称重新映射到 initialValues 下,以便 redux-form 可以选择它,例如:
import MyComponent from './MyComponent'
import { gql, graphql } from 'react-apollo'
const currentUser = gql`
query viewer {
viewer {
firstName
lastName
}
}
`
export default graphql(currentUser, {
props: ({ ownProps, data: { loading, viewer } }) => ({
loading,
initialValues: {
firstName: viewer && viewer.firstName,
lastName: viewer && viewer.lastName,
},
})
)(MyComponent)