What are you trying to do? (please be as specific as possible and include relevant screenshots, code snippets, and reproduction steps)
I’m trying to know if I’m going to be able to use Plasmic for my use cases.
In my use cases, all of the auth/RBAC logic is handled by my backend.
What have you tried so far? (please link relevant docs and other forum posts)
I created a simple login page and a content page to redirect logged in users. I also created a Global Context to provide the data and actions.
What is the problem with this approach?
When a page is rendered, the Global Context is also re-rendered, so the data doesn’t persist.
Here’s the Global Context I created:
import { useState, useMemo } from "react";
import { DataProvider, GlobalActionsProvider } from "@plasmicapp/loader-react";
export const MyGlobalContext = ({ children }) => {
const [tokens, setTokens] = useState(null);
const logIn = async (email, password) => {
// get tokens from my backend
var tokensInfo = await fetch(...)
setTokens(tokensInfo);
return tokensInfo
};
const actions = useMemo(
() => ({
logIn: (email, password) => logIn(email, password)
.then(token => token)
.catch(error => console.error({error})),
}),
[]
);
return (
<GlobalActionsProvider contextName="MyGlobalContext" actions={actions}>
<DataProvider name="tokens" data={tokens}>
{children}
</DataProvider>
</GlobalActionsProvider>
);
};
Thanks in advance!