0

考虑这个使用版本的例子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
 }))

记录在案的版本演示代码>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)

第一种模式仍然可能吗?如果是这样,它是如何工作的?它在任何地方都有记录吗?我看到我的组件道具中有一个initialize调度程序。是这样吗?

4

1 回答 1

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)
于 2017-06-01T20:29:30.150 回答