【问题标题】:How to evaluate the current field value using a FieldArray?如何使用 FieldArray 评估当前字段值?
【发布时间】:2017-12-07 17:41:04
【问题描述】:

是否可以在使用FieldArray 时获取字段的当前值?例如,我有以下代码:

import React from 'react';
import { Form, ArrayField, reduxForm } from 'redux-form';
import { Container, Row, Col } from 'reactstrap';
import { connect } from 'react-redux';

const mapStateToProps = (state) => ({
  initialValues: {
    customers: state.currentOffice.customers
  }
});

const renderCustomers = (fields, meta) => (
  <Container>
  {
    fields.map((customer, index) => (
      <Row>
        <Col lg='6'>
          <span>{ /* I need the current customer name be shown here */ }</span>

          <Field
            name={`${customer}.name`}
            component='input'
            className='form-control'
          />
        </Col>
      </Row>
    ))
  }
  </Container>
);

const MyForm = reduxForm({ form: 'customersForm' })((props) => (
  <Form onSubmit={(e) => e.preventDefault()}>
    <FieldArray name='customers' component={renderCustomers} />
  </Form>
));

export default connect(mapStateToProps)(MyForm);

我需要在第 18 行显示客户名称,但是当我尝试评估 JSX 花括号之间的客户名称时,我收到的字面意思是 customers[0].name

是否可以使用 redux-form 来实现?

【问题讨论】:

  • 可能你打错字了,因为参数是customer,而你使用的是customers[0].names

标签: reactjs redux react-redux redux-form


【解决方案1】:

我认为fields.get(index) 是您寻求的答案。

【讨论】:

    猜你喜欢
    • 2012-03-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多