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

[Doc] Fix missing DataProviderContext in Querying the API chapter #5988

Merged
merged 2 commits into from
Mar 3, 2021
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 39 additions & 1 deletion docs/Actions.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,45 @@ const UserProfile = ({ userId }) => {
};
```

**Tip**: The `dataProvider` returned by the hook is actually a *wrapper* around your Data Provider. This wrapper dispatches Redux actions on load, success and failure, which keeps track of the loading state.
**Tip**: The `dataProvider` returned by the hook is actually a *wrapper* around your Data Provider. This wrapper updates the Redux store on success, and keeps track of the loading state. In case you don't want to update the Redux store (e.g. when implementing an autosave feature), you should access the raw, non-wrapper Data Provider from the `DataProviderContext`:
fzaninotto marked this conversation as resolved.
Show resolved Hide resolved

```diff
import * as React from 'react';
-import { useState, useEffect } from 'react';
+import { useState, useEffect, useContext } from 'react';
-import { useDataProvider, Loading, Error } from 'react-admin';
+import { DataProviderContext, Loading, Error } from 'react-admin';

const UserProfile = ({ userId }) => {
- const dataProvider = useDataProvider();
+ const dataProvider = useContext(DataProviderContext);
const [user, setUser] = useState();
const [loading, setLoading] = useState(true);
const [error, setError] = useState();
useEffect(() => {
dataProvider.getOne('users', { id: userId })
.then(({ data }) => {
setUser(data);
setLoading(false);
})
.catch(error => {
setError(error);
setLoading(false);
})
}, []);

if (loading) return <Loading />;
if (error) return <Error />;
if (!user) return null;

return (
<ul>
<li>Name: {user.name}</li>
<li>Email: {user.email}</li>
</ul>
)
};
```

## `useQuery` Hook

Expand Down