【问题标题】:dispatch a custom action on redux-form when onchange select在 onchange 选择时在 redux-form 上调度自定义操作
【发布时间】:2017-02-09 17:10:03
【问题描述】:

给定一个自定义组件:

export const RenderDropDown = (props) => {
    const {values, input, label, type, meta: {touched, error, active}} = props;

    var options = function (value, index) {
        return <Option key={index}
                       value={value} />
    };

    return(
        <div>
        <select {...input}>
            <option value="">--- Select a nationality ---</option>
            {values.map(options)}
        </select>
        </div>
    )
}

包装在 Redux 形式的 Field 组件中

<Field name="nationality" component={RenderDropDown} values={props.nationalities}/>

我希望当select 触发“onChange”事件时,除了 Redux-form 操作(AKA @@redux-form/change)之外,还会调度一个自定义操作

我怎样才能做到这一点?

我试图通过 props 传递函数来调用并在&lt;select&gt; 标签上设置一个 onChange 处理程序,但这会覆盖 redux-form onChange。

任何帮助表示赞赏!

【问题讨论】:

    标签: react-redux redux-form


    【解决方案1】:

    您需要在自己的自定义onChange 中手动调用redux-form onChange 来更新redux-form 值。像这样:

    const { onChange, ... } = props;
    ...
    <select onChange={(e) => {
        const val = e.target.value
        // whatever stuff you want to do
        onChange(val)
    }} />
    

    【讨论】:

    • 嘿@inostia 感谢您的回答。我自己找到了答案。看起来它已经实现了,但我以某种方式错过了它:github.com/erikras/redux-form/releases 事件回调! - 您现在可以将 onChange、onBlur、onFocus 等道具传递给 Field...
    • 感谢@VicensFayos。我使用的是 redux-form 5.x.x,但现在已经更新到 6.5.x。因此,如果您将 onChange 直接传递给 ,它会在您之后自动调用 redux-form onChange 吗? (即更新 redux-form 存储中的字段值)
    猜你喜欢
    • 2019-12-24
    • 2017-09-22
    • 2013-07-25
    • 2016-05-22
    • 1970-01-01
    • 2017-11-09
    • 1970-01-01
    • 2018-06-29
    • 1970-01-01
    相关资源
    最近更新 更多