小编Wil*_*ley的帖子

你如何有条件地在 react-admin 的“显示”组件中显示字段?

有些字段我只想显示它们是否有值。我希望这样做:

<Show {...props} >
  <SimpleShowLayout>
    { props.record.id ? <TextField source="id" />: null }
  </SimpleShowLayout>
</Show>
Run Code Online (Sandbox Code Playgroud)

但这不起作用。我可以通过使每个字段成为高阶组件来使其工作,但我想做一些更清洁的事情。这是我拥有的 HOC 方法:

const exists = WrappedComponent => props => props.record[props.source] ?
  <WrappedComponent {...props} />: null;

const ExistsTextField = exists(TextField);

// then in the component:

<Show {...props} >
  <SimpleShowLayout>
    <ExistsTextField source="id" />
  </SimpleShowLayout>
</Show>
Run Code Online (Sandbox Code Playgroud)

这正确地显示了值,但剥离了标签。

conditional react-admin

3
推荐指数
1
解决办法
4821
查看次数

标签 统计

conditional ×1

react-admin ×1