【问题标题】:react-admin this.props.record is undefinedreact-admin this.props.record 未定义
【发布时间】:2020-06-08 10:38:51
【问题描述】:

我有一个Edit 组件,我把它变成了一个类,看起来像这样:

<Edit {...this.props} >
    <SimpleForm
      toolbar={null}
    >
      <TextField source='email'/>
      <ArrayField source='allComponents'>
        <Datagrid>
          <TextField label='ID' source='id'/>
          <TextField label='Component Name' source='name'/>
          <RadioButtons label='Access'
                        source='value'
                        onChange={this.handleChange}
                        value={this.props.record.value}
                        name={this.props.record.name}
          />
        </Datagrid>
      </ArrayField>
      <SaveButton onClick={this.handleSubmit}/>
    </SimpleForm>
  </Edit>

我还有另一个名为 RadioButtons 的组件,如下所示:

export const RadioButtons = ({ handleChange }) => {
    const { value, name } = this.props
    return <FormControl>
      <RadioGroup row value={value.toString()} name={name.toString()}>
        {choices.map(c => <FormControlLabel
            control={<Radio/>}
            label={c.name}
            key={c.id}
            value={c.value}
            style={{ flexDirection: 'column-reverse', padding: '10px 20px' }}
            onChange={handleChange}
          />
        )}
      </RadioGroup>
    </FormControl>
}

Edit 组件呈现与我单击的用户相关的组件列表。此列表中的每个组件都有 3 个单选按钮,可能有 3 个值,它呈现 choices 数组:

const choices = [
  { id: '1', name: 'no access', value: '0' },
  { id: '2', name: 'read access', value: '1' },
  { id: '3', name: 'full access', value: '2' }
]

我正在尝试将输入值传递给RadioButtons,它是与 react-admin 的单选按钮不同的组件。

我该怎么做?通常是source='value',但这不是 react-admin 的组件。此外,当尝试访问记录对象时,this.props.record 中似乎不存在它(未定义)。

【问题讨论】:

    标签: react-admin


    【解决方案1】:

    根据https://marmelab.com/react-admin/Fields.html

    record:包含要显示的属性的对象。 ,以及其他组件将该道具注入到他们的孩子

    因此,只要您的组件看起来像在这里一样包装在 DataGrid 中,您就应该是正确的。但是你也需要像这样拿起记录:

    export const RadioButtons = ({ handleChange, record }) => {

    【讨论】:

    • 我认为问题在于如何自己处理记录数据。
    猜你喜欢
    • 1970-01-01
    • 2020-02-11
    • 2021-04-11
    • 1970-01-01
    • 2017-09-24
    • 2021-05-15
    • 1970-01-01
    • 1970-01-01
    • 2019-01-02
    相关资源
    最近更新 更多