2017-06-01 58 views
0

我使用ReduxForm:ReduxForm - 动态有两种形式的连接参数

  • 内部情态动词
  • 内标签。

取决于窗体的类型,我必须调整我的reduxForm'connect'参数。

有了标签,我需要:

  • destroyOnUnmount
  • enableReinitialize
  • [...]

随着模态,我不希望他们。

我的表单由Props获取表单的类型,但我无法用它来调整我的reduxForm。

export defaut ReduxForm({ 
    form : 'testForm', 
    destroyOnUnmount : false, 
    enableReinitialize: true , 
    [...] 
    })(MyForm) 

我不知道如何去适应这个。

感谢

回答

1

我不是100%地肯定你问但我认为我有一个非常好的线索:如果您的表单位于Tab中,并且如果您不添加它们,您希望能够添加destroyOnUnmountenableReinitialize道具r形式在模态内,对吗?

您可以通过从react-redux包装与connectreduxForm -wrapped组件,然后使用mapStateToProps确定以通为redux-form其支柱值做到这一点。事情是这样的:

const mapStateToProps = (state, ownProps) => { 
    const { insideTab } = ownProps 

    return { 
    ...ownProps, 
    destroyOnUnmount: !insideTab, 
    enableReinitialize: !!insideTab 
    } 
} 

export default connect(mapStaToProps)(
    ReduxForm({ form : 'testForm' })(MyForm) 
) 

然后你可以这样使用

// this creates a container with 
// destroyOnUnmount=false 
// and 
// enableReinitialize=true 
<MyFormContainer insideTab /> 

// this creates a container with 
// destroyOnUnmount=true 
// and 
// enableReinitialize=false 
<MyFormContainer /> 

希望这有助于!

+0

完美!非常感谢 :) – GreGGus

0

而不是出口了Redux形式,导出返回它的功能:

export default isTab => ReduxForm({ 
    form : 'testForm', 
    destroyOnUnmount : !!isTab, 
    enableReinitialize: !!isTab , 
    [...] 
})(MyForm) 

然后

import makeMyForm from 'MyForm' 
const TabForm = makeMyForm(true); 
const ModalForm = makeMyForm();