使用版本5.3.2
考虑this example:
@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。那是这样的吗?
答案 0 :(得分:2)
我会给你一个阿波罗特定的答案,虽然你没有在你的问题中提到阿波罗,只是在标签中。看起来你的意思是。
使用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)