代码之家  ›  专栏  ›  技术社区  ›  Shawn Matthews

如何在Redux表单中将prop传递给onChange函数?

  •  0
  • Shawn Matthews  · 技术社区  · 7 年前

    因此,假设我有以下组件(这是伪代码,但希望能用更短的阅读格式来说明我的问题)。。。

    class Example extends React.Component {
      onChange = () => {
        store.dispatch({ type: 'FETCH_VENUE', location: locationValue })
      }
    
      render() {
        console.log(this.props.id) // "2"
        return (
          <Field id="salesArea" name="orderHeader.salesArea" type="select" onChange={this.onChange}
                 component={SelectComponent} />
        )
      }
    }
    
    export default Example;
    

    如果容器组件ExampleContainer中有一个道具,它在此处提供一个值。道具id.如何将其传递为locationValue,因此基本上相当于

    location: this.props.id

    无需创建反模式。

    这道具。id使用HOC中的formValueSelector返回salesArea的当前值。

    如果我设置了位置,我的代码工作得很好,例如:“2”,但有没有办法将道具传递给此调度?

    1 回复  |  直到 7 年前
        1
  •  0
  •   Shawn Matthews    7 年前

    对于任何一个偶然发现这一点的人。我解决了它如下。

    我在简单表单组件中添加了以下函数。

    onVenueChange = (event, venueId) => {
        console.log(venueId);
        this.props.onVenueChange(venueId)
    }
    

    然后我在中这样称呼它<字段/>

    call onChange={this.onVenueChange}
    

    在作为mapDispatch一部分的HOC中,我有以下内容。它调度动作。

    onVenueChange: venueId => dispatch({type: 'FETCH_EDITABLEORDERS', venueId: venueId })