score:1
Accepted answer
I found the answer. To make work, you need to pass the authorization token as a parameter in the dataProvider function wrapper, and using react-cookie, you can use all the power of states and React Hooks. It's like magic:
import React from 'react';
import { Admin, Resource } from 'react-admin';
import Dashboard from './Dashboard';
import hasuraDataProvider from 'ra-data-hasura';
import authProvider from './authProvider';
import {useCookies} from "react-cookie";
import { AssociadoList, AssociadoShow } from "./Associado";
import portugueseMessages from 'ra-language-portuguese'
const dataProvider = function (authToken) {
const dataProvider = hasuraDataProvider("http://localhost:8080", { "content-type": "application/json", "Authorization": "Bearer " + authToken});
return dataProvider;
}
const messages = {
'pt': portugueseMessages,
};
const i18nProvider = locale => messages[locale];
function App() {
const [cookies] = useCookies(['authToken']);
return (
<Admin
dataProvider={dataProvider(cookies.authToken)}
authProvider={authProvider}
dashboard={Dashboard}
locale="pt" i18nProvider={i18nProvider}
>
<Resource name="adear.associado" list={AssociadoList} options={{ label: 'Associado' }} show={AssociadoShow} />
</Admin>
);
}
export default App;
score:1
My take is that you have a small issue with your custom dataProvider.
You are trying to enhance the hasura data provider, but by doing so, you are introducing a small bug.
import React from 'react';
import { Admin, Resource } from 'react-admin';
import Dashboard from './Dashboard';
import hasuraDataProvider from 'ra-data-hasura';
import authProvider from './authProvider';
import Cookies from 'universal-cookie';
import { AssociadoList, AssociadoShow } from "./Associado";
import portugueseMessages from 'ra-language-portuguese'
const dataProvider = function () {
const cookies = new Cookies();
// hasuraDataProvider is a factory, which returns a function
const dataProvider = hasuraDataProvider(process.env.REACT_APP_HASURA_URL, { "content-type": "application/json", "Authorization": "Bearer " + cookies.get("AuthToken") });
return dataProvider; // Here you are returning the real dataprovider
}
const messages = {
'pt': portugueseMessages,
};
const i18nProvider = locale => messages[locale];
function App() {
return (
<Admin
{/* dataProvider is a function which returns the hasura dataProvider, but React Admin expects to get your function results instead! */}
dataProvider={dataProvider}
authProvider={authProvider}
dashboard={Dashboard}
locale="pt" i18nProvider={i18nProvider}
>
<Resource name="adear.associado" list={AssociadoList} options={{ label: 'Associado' }} show={AssociadoShow} />
</Admin>
);
}
export default App;
IMHO, the fix is as simple as calling your dataProvider function when passing the prop to the admin.
- dataProvider={dataProvider}
+ dataProvider={dataProvider()}
Source: stackoverflow.com
Related Query
- The dataProvider is probably wrong for 'GET_LIST'
- Why do I get a warning that says " each child in a list should have a unique key prop" but I already have a unique key prop for the child component
- How do I filter a list of data by id for my react component to get the name of the object
- How do I get the username for an id from a list of ids
- How to get VSCode to show TypeScript errors for files *not* open in the editor?
- how to get list of all registered synthetic event handlers/listeners for a React Component?
- How to get the marquee effect with list view in react native?
- React: using index as key for items in the list
- How to wait for the render to complete to get useContext when using useEffect?
- React native buld failed with the following message Failed to list versions for com.facebook.react:react-native
- React renders the wrong data when deleting the item in the middle of the list
- Axios get request returns undefined for the first time in React
- How to locate react-leaflet map to user's current position and get the borders for this map?
- Need to get the last value of onChange for a slider React
- Can I use React.Fragment for list rendering while have the "key" assigned?
- how to get the url for a package in npmjs.com instead of installing with npm
- Why do I get a 404 for half a second when clicking a next.js `Link` that just calls an API endpoint and then navigates to the correct page?
- page title not available with jest/enzyme for testing react app, how to get the title?
- Get the user IP for Stripe registration, using Reactjs and Firebase
- Port redux state to component state when the component mounts (without need for GET request)
- redux-form get the name of form for the input
- How to get full test coverage for the react code
- How to get the InputNumber name in antdesign for react?
- How do I get the base url of the site for twitter:image meta tag?
- Get the max value for a date from an array of objects
- How to get the html elements from Contentful Raw Data for RSS-feeds (gatsby-plugin-feed)
- Each child list key is unique yet I still get the error in my console. Reactjs
- [Webpack][React] On SSR with code-splitting how I can get the list of chunks needed by the page?
- A function to get the absolute position for a chutes and ladders game?
- ClickEvent fires twice and returns the wrong value for .contains()
More Query from same tag
- How to change state of a component when the component is scrolled in view?
- React TransitionGroup componentWillLeave does nothing
- Testing nested JSX with React Testing Library
- Nested Routes in React js not working
- Canonical approach for implementing `combineReducers` in existing application
- Refactor Component to FunctionComponent with conditional style (onMouseEnter/Leave)
- Passing Parameters to Components in React Native
- Javascript Convert an Object to a URL
- Redux Action Creator Causing Infinite Loop
- onClick of a btn i want to get it's sibling(input box) value with useRef and copyToClipboard, setState for that btn to Copied
- the routes change but the pages are not shown in my react app
- Passing user state to child component in React
- How to use SCSS variables into my React components
- Preventing setState rerenders entire functional component
- Change resultant's key name in cube.js
- How to make the part of search suggestion bold based on the user input?
- Redux Spread Operator vs Map
- Material UI auto complete value and onChange
- How to add fields to array that have no property react js
- KeystoneJS can't update post
- React MDBTable render each item in table
- ERROR: use callback in setstate when referencing the previous state
- Rendering a single element from an array of data in React
- How to capture the backspace event in onChange tag of Form for Reactjs applications
- React-select Async fetch
- react big calendar repeated date
- React how to update props for item clicked and not all items
- New Object Property in React
- Playing audio with useEffect hook on page load
- google places api text not working in mui TextField