Global Context not passing via plugin or provider #2609
-
Anyone please help I'm new to fresh. I'm trying to figure out. how to pass global context properly to islands so that I can operate upon it The below is the global context that I'm trying to use import { createContext, FunctionalComponent } from "preact";
import { useReducer } from "preact/hooks";
interface GlobalState {
logined: boolean;
user: string;
}
const initialState: GlobalState = {
logined: false,
user: "",
};
const reducer = (state: GlobalState, action: { type: string; payload: any }): GlobalState => {
switch (action.type) {
case "SET_LOGINED":
return { ...state, logined: action.payload };
case "SET_USER":
return { ...state, user: action.payload };
default:
return state;
}
};
const GlobalContext = createContext<{
state: GlobalState;
dispatch: (action: { type: string; payload: any }) => void;
}>({
state: initialState,
dispatch: () => null,
});
const GlobalProvider: FunctionalComponent<{ children: preact.ComponentChildren }> = ({ children }) => {
const [state, dispatch] = useReducer(reducer, initialState);
return (
<GlobalContext.Provider value={{ state, dispatch }}>
{children}
</GlobalContext.Provider>
);
};
export { GlobalProvider, GlobalContext }; The below is the plugin I wrote import { Plugin, PluginRenderContext } from "$fresh/server.ts";
import Root from "./components/root.tsx";
import { renderToString } from "https://cdn.skypack.dev/[email protected]";
const global_context_plugin: Plugin = {
name: "global_context",
render(ctx: PluginRenderContext) {
const res = ctx.render();
const wrappedResult = (
<Root>
{res}
</Root>
);
return {
htmlText: renderToString(wrappedResult),
requiresHydration: true,
};
},
};
export default global_context_plugin; if anyone knows it please help |
Beta Was this translation helpful? Give feedback.
Answered by
marvinhagemeister
Aug 2, 2024
Replies: 1 comment 3 replies
-
Passing data from the server to islands running in the browser via context is not supported. If you want to pass something to an island it needs to be passed it as a prop. There is no other way to pass data to islands in Fresh. |
Beta Was this translation helpful? Give feedback.
3 replies
Answer selected by
siddh34
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Passing data from the server to islands running in the browser via context is not supported. If you want to pass something to an island it needs to be passed it as a prop. There is no other way to pass data to islands in Fresh.