reactjsparametersreact-admindataprovider

React-admin: How to Pass more parameters to a dataProvider for type GET_LIST


I want to pass one custom parameter to the Admin's dataProvider for type 'GET_LIST'.

I have something like this in App.js:

<Admin dataProvider={dataProvider}> 
    <Resource name="posts" list={PostList} myCustomAttr={"10"} /> 
    <Resource name="users" list={UserList} myCustomAttr={"15"} /> 
</Admin>

And 'dataProvider' is a custom dataProvider and I want to have the 'myCustomAttr' when it gets called.

So my custom dataProvider could look like this:

export default (type, resource, params) => {

    if (type == 'GET_LIST') {
       if (params.myCustomAttr == '10') {
           //Do something
       }
    }
}

Solution

  • Ok, I've solved this by using the List filter prop:

    <Admin dataProvider={dataProvider}> 
        <Resource name="posts" list={PostList} options={{ myCustomAttr: "10" }} /> 
        <Resource name="users" list={UserList} options={{ myCustomAttr: "15" }} /> 
    </Admin>
    

    And in the UserList (like) component:

    export const UserList = function(props) {
        return <List {...props} filter={{myCustomAttr:props.options.myCustomAttr}} >
            //...
        </List>
    };
    

    So, this way, I get what I want in the dataProvider.

    export default (type, resource, params) => {
    
        if (type == 'GET_LIST') {
           if (params.filter.myCustomAttr == '10') {
               //Do something
           }
        }
    }