简体   繁体   English

在react-admin中访问组件的值

[英]Access the values of components in react-admin

I want to access values of components. 我想访问组件的值。 Because I'll use it to access to another data. 因为我将使用它来访问其他数据。 For example; 例如;

<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>

I want to access it in the filter method. 我想在filter方法中访问它。 Is there a way to do that? 有没有办法做到这一点?

You can use something like this: 您可以使用如下形式:

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>
); 

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

You're not limited to hide/show things. 您不仅可以隐藏/显示事物。 In your case, you should be able to avoid using state and directly access formData.serviceTypes 就您而言,您应该能够避免使用state并直接访问formData.serviceTypes

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM