【问题标题】:Where does a Field component in Redux form get the input prop from?Redux 表单中的 Field 组件从哪里获取输入道具?
【发布时间】:2016-10-17 23:03:28
【问题描述】:

page 了解 redux-forms,但不明白如何将 input props 提供给 Field 组件,如下所示:

class MyCustomInput extends Component {
  render() {
    const { input: { value, onChange } } = this.props
    return (
      <div>
        <span>The current value is {value}.</span>
        <button type="button" onClick={() => onChange(value + 1)}>Inc</button>
        <button type="button" onClick={() => onChange(value - 1)}>Dec</button>
      </div>
    )
  }
}

MyCustonInput 从哪里获得input 属性?该示例仅在下面显示:

import MyCustomInput from './MyCustomInput'

...

<Field name="myField" component={MyCustomInput}/>

【问题讨论】:

    标签: redux react-redux redux-form


    【解决方案1】:

    Field 是一个高阶组件,它将这些属性应用于传入component 的任何内容。它有效地确定了该字段需要什么值以及它应该如何连接到 redux 存储,具体取决于您传入的组件以及Field 的其他可能属性。

    源代码可能是最好的事实来源。您可以在 createFieldProps 函数中查看输入的定义位置: https://github.com/erikras/redux-form/blob/master/src/createFieldProps.js

    【讨论】:

      猜你喜欢
      • 2020-07-19
      • 2020-05-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-07-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多