【问题标题】:Material ui textfield ith redux form issue材料ui文本字段与redux表单问题
【发布时间】:2018-02-23 17:58:45
【问题描述】:

我在这里遇到了一个我无法处理的问题 我有一个与 redux 表单兼容的文本字段 像这样:

const renderTextField = props => (
  <TextField {...props} />
);

我是这样使用它的:

<Field
          id="searchCif"
          name="searchCif"
          component={renderTextField}
          floatingLabelText={SEARCHVIEW_HINT_CIF}
          floatingLabelFixed={false}
          value
        />

然后我在我的容器中写这个:

import { reduxForm } from 'redux-form/immutable';
import { connect } from 'react-redux';
// import { injectIntl } from 'react-intl';
import SearchDefaultView from './views/searchDefaultView';

import { requestCustomerInfo } from './actions/customerActions';

export const mapDispatchToProps = dispatch => (
  {
    requestCustomerInfo: formData =>
      dispatch(requestCustomerInfo(formData))
  }
);

const SearchDefaultReduxForm = reduxForm({
  form: 'customerInfo',  // a unique identifier for this form
})(SearchDefaultView);

const SearchDefaultContainer = connect(
  null,
  mapDispatchToProps
)(SearchDefaultReduxForm);

export default SearchDefaultContainer;

但是当我写值并提交我的表单时,表单没有值。我错过了什么?

根据我使用的文档:

const renderTextField = ({
                           input,
                           label,
                           meta: { touched, error },
                           ...custom
                         }) =>
  <TextField
    hintText={label}
    floatingLabelText={label}
    errorText={touched && error}
    {...input}
    {...custom}
  />

const SearchDefaultView = (props) => {
  const { requestCustomerInfo, handleSubmit } = props;
  return (
    <form onSubmit={handleSubmit(requestCustomerInfo)}>
      <Menu
        autoWidth={false}
        style={styleSearchMenu}
      >
        <Divider />
        <Field
          id="searchCif"
          name="searchCif"
          component={renderTextField}
          floatingLabelText={SEARCHVIEW_HINT_CIF}
          floatingLabelFixed={false}
        />
        <br />
        <Field
          id="searchAFM"
          name="searchAFM"
          component={renderTextField}
          floatingLabelText={SEARCHVIEW_HINT_AFM}
          floatingLabelFixed={false}
        />
        <br />
        <RaisedButton
          type="submit"
          fullWidth
          primary
          label={SEARCHVIEW_SEARCH}
        />
      </Menu>
    </form>
  );
};

但它在 ...custom 处显示编译错误

【问题讨论】:

    标签: javascript reactjs redux material-ui redux-form


    【解决方案1】:

    我认为你没有使用组件的 onChange 属性。

    onChange:文本字段值改变时触发的回调函数。

    您应该分派更改并更新 redux 容器中的数据。

    http://www.material-ui.com/#/components/text-field

    【讨论】:

    • 值属性错误。删除它或将它附加到redux中的变量
    • 我听不懂你在说什么。该值由 redux 表单自动附加。使用它就是这样
    • 值没有赋值,即使你是从 redux 传递上面的 props。
    【解决方案2】:

    当您想为 Redux-Form 使用自定义字段时,Redux-form 可以让您访问 onChange 等属性,还可以访问其他元数据(例如,如果表单有是否被触摸)。这些不同类型的道具根据类型进行分组。对您来说有趣的部分是与普通输入元素相关的所有属性(如onChangevaluetype)都分组在props.input 中。因此,要将 那些 传递给 &lt;TextField /&gt; 组件,您不能直接在 props 上使用扩展运算符 (...)。您必须在props.input 上使用它。

    const renderTextField = props => (
      <TextField {...props.input} />
    );
    

    您可能还需要处理这样一个事实:&lt;TextField /&gt; 所期望的 onChange 方法不一定与 Redux-form 为您提供的 onChange 方法具有相同的签名。所以你可能需要做一些手工工作才能让它们一起工作,类似于我的outlined in this post。您必须分别阅读 Redux-Form 和 Material-UI TextField 的 onChange 的文档。

    您可能还想知道对于material-ui 组件,实际上已经存在library that has done that manual work for you: redux-form-material-ui

    【讨论】:

    • 非常感谢您的反馈,但 erikras 在他的材料 UI 文档中没有使用 props.input。我的状态好像没有更新
    • 据我所知,他做到了。请参阅链接。他解构了方法参数并挑选出输入部分并将其展开。 redux-form.com/7.0.4/examples/material-ui
    • renderTextfield 未编译。它在...习惯上粉碎。你可以自己检查一下
    • 什么代码没有编译?我的例子中的那个?文档中的那个?您可能必须使用当前代码更新问题。你现在的情况太不清楚了。
    • 我用文档中的那个编辑了 renderTextField 但它没有编译
    猜你喜欢
    • 1970-01-01
    • 2017-09-28
    • 1970-01-01
    • 2021-04-06
    • 1970-01-01
    • 2021-06-23
    • 1970-01-01
    • 1970-01-01
    • 2018-09-28
    相关资源
    最近更新 更多