如何使用FieldArray评估当前字段值?

时间:2017-12-07 17:41:04

标签: reactjs redux react-redux redux-form

使用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实现?

1 个答案:

答案 0 :(得分:0)

I think fields.get(index) is the answer you seek.