在react-admin中访问组件的值

时间:2018-07-16 06:31:19

标签: reactjs admin-on-rest react-admin

我想访问组件的值。因为我将使用它来访问其他数据。例如;

<ArrayInput source='services'>
    <SimpleFormIterator>
        <ReferenceInput label="Service Type"
            source="serviceType"
            reference="servicetypes"
            validate={required()}>
            <SelectInput optionText={GAMMA_CONSTANTS.SOURCE_ID} />
        </ReferenceInput>
        {(this.state.serviceTypes && this.state.serviceTypes.length > 0) ?
            this.state.serviceTypes.filter(serviceType => {
                    return serviceType.id === (**source="serviceType"**)
                })[0]["datapointtype"].map((feature, index) => {
                return <TextInput source={index} label="deneme" />
            })
            : null}

    </SimpleFormIterator>
</ArrayInput>

我想用filter方法访问它。有办法吗?

1 个答案:

答案 0 :(得分:0)

您可以使用以下内容:

import { FormDataConsumer } from 'react-admin';

const PostEdit = (props) => (
    <Edit {...props}>
        <SimpleForm>
            <BooleanInput source="hasEmail" />
            <FormDataConsumer>
                {({ formData, ...rest }) => formData.hasEmail &&
                     <TextInput source="email" {...rest} />
                }
            </FormDataConsumer>
        </SimpleForm>
    </Edit>
); 

参考:https://marmelab.com/react-admin/Inputs.html#hiding-inputs-based-on-other-inputs

您不仅可以隐藏/显示事物。就您而言,您应该能够避免使用状态并直接访问formData.serviceTypes