Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[RFR] Data hooks #3181

Merged
merged 12 commits into from
May 4, 2019
Merged

[RFR] Data hooks #3181

merged 12 commits into from
May 4, 2019

Conversation

fzaninotto
Copy link
Member

@fzaninotto fzaninotto commented Apr 30, 2019

  • Add useDataProvider hook
  • [BC Break] withDataProvider no longer injects the dispatch prop
  • Add useQuery hook
  • Add useMutation hook
  • Document all that
import React, { useState } from 'react';
import { useDispatch } from 'react-redux';
import { useDataProvider, showNotification } from 'react-admin';

const PostList = () => {
     const [posts, setPosts] = useState([])
     const dispatch = useDispatch();
     const dataProvider = useDataProvider();

     useEffect(() => {
         dataProvider('GET_LIST', 'posts', { filter: { status: 'pending' }})
           .then(({ data }) => setPosts(data))
           .catch(error => dispatch(showNotification(error.message, 'error')));
     }, [])

     return (
         <Fragment>
             {posts.map((post, key) => <PostDetail post={post} key={key} />)}
         </Fragment>
    }
}
import { useQuery } from 'react-admin';

const payload = {
   pagination: { page: 1, perPage: 10 },
   sort: { field: 'username', order: 'ASC' },
};
const UserList = () => {
    const { data, total, loading, error } = useQuery('GET_LIST', 'users', payload);
    if (loading) { return <Loading />; }
    if (error) { return <p>ERROR</p>; }
    return (
        <div>
            <p>Total users: {total}</p>
            <ul>
                {data.map(user => <li key={user.username}>{user.username}</li>)}
            </ul>
        </div>
    );
};
import { useMutation } from 'react-admin';

const ApproveButton = ({ record }) => {
    const [approve, { loading }] = useMutation(
        'UPDATE',
        'comments',
         { id: record.id, data: { isApproved: true } }
    );
    return <FlatButton label="Approve" onClick={approve} disabled={loading} />;
};

@fzaninotto fzaninotto changed the title [WIP] Data hooks [BC Break] [WIP] Data hooks Apr 30, 2019
@fzaninotto fzaninotto added this to the 3.0.0 milestone Apr 30, 2019
@fzaninotto fzaninotto changed the title [BC Break] [WIP] Data hooks [BC Break] [RFR] Data hooks May 3, 2019
@fzaninotto
Copy link
Member Author

Switching to RFR

*
* In addition to the 3 parameters of the dataProvider function (verb, resource, payload),
* the injected dataProvider prop accepts a fourth parameter, an object literal
* which may contain side effects, of make the action optimistic (with undoable: true).
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

s/of/or/

As requested by djhi at #3181 (comment)
Copy link
Collaborator

@djhi djhi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome :)

@djhi djhi merged commit 91bfb83 into next May 4, 2019
@djhi djhi deleted the data-hooks branch May 4, 2019 16:28
@fzaninotto fzaninotto mentioned this pull request May 7, 2019
40 tasks
@fzaninotto fzaninotto changed the title [BC Break] [RFR] Data hooks [RFR] Data hooks May 7, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants