简体   繁体   English

React-Admin:如何在没有 `source` 的情况下访问值?

[英]React-Admin: How can I access a value without `source`?

I'm trying to create a dynamic button in react-admin .我正在尝试在react-admin创建一个动态按钮。
For each button it hits a unique address, how can I do that?对于每个按钮,它都会点击一个唯一的地址,我该怎么做?

Unfortunately, I have this error: props.username is undefined .不幸的是,我有这个错误: props.username is undefined

export const LoginCredentialList = (props) => (
  <List {...props} pagination={<PostPagination/>}>
    <Datagrid>
       <TextField label="Username" source="username" />
       <Button label="Re-invoke Login"
          onClick={() => {       
            axios.get("http://localhost:8080/admin/" + somehow_read_username_here)
                 .then((res)=>console.log(res));
             }}
        />
    </Datagrid>
  </List>
);

Here is the parent , where the above component is being called or used:这是调用或使用上述组件的parent

class SPanel extends React.Component {
  render() {
    return (
      <div>
        <Admin dataProvider={restDataProvider}>
          <Resource name="loginCredential" list={LoginCredentialList} />
            ...

I have not worked with react-admin before, but after quickly skimming through its documentation what I understnad is that我之前没有与react-admin过,但是在快速浏览其文档之后,我不明白的是

  • the props you receive in your LoginCredentialList component has list of records您在LoginCredentialList组件中收到的props具有记录列表
  • Datagrid iterates over this list and passes individual record to children Field components Datagrid遍历此列表并将单个record传递给子Field组件
  • Field components receives record as prop injected by react-admin Field组件接收record作为react-admin注入的 prop

Though react-admin provides many standard Field components, it does not have a ButtonField .尽管react-admin提供了许多标准的Field组件,但它没有ButtonField

So you can write your own Field component.因此,您可以编写自己的Field组件。 ( https://marmelab.com/react-admin/Fields.html#writing-your-own-field-component ) ( https://marmelab.com/react-admin/Fields.html#writing-your-own-field-component )

Something like:就像是:

const ButtonField = ({source, record}) => (
    <Button label="Re-invoke Login" onClick={()=>{
        axios
            .get("http://localhost:8080/admin/"+record[source])
            .then((res)=>console.log(res));
    }}/>
);

And then use it your main component as然后将其用作您的主要组件

export const LoginCredentialList = (props) => (
    <List {...props} pagination={<PostPagination/>}>
        <Datagrid>
            <TextField label="Username" source="username" />
            <ButtonField source="username" />
        </Datagrid>
    </List>
);

Try this out.试试这个。

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

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