Suggestion: global addContextProps hook
See original GitHub issueI have a need for global data that is available to all pages. I am aware that this is possible within the server entrypoint of the application, however that is not a perfect solution in all cases for the following reasons:
- The server file entrypoint is not a module context, so does not have access to env variables via
import.meta.env
. One could maybe rundotenv.config
to get around this, but it feels inconsistent and hacky to be accessing env vars in two different ways - Since the server entrypoint is not processed by Vite, it complains when attempting to import files using ES module syntax. This can lead to inconsistencies, confusion around which files can / are supposed to be used where.
- Data populated in the server entrypoint is not available in prerender hooks. This means any logic used here to populate the initial contextProps needs to be replicated in prerender hooks.
I am proposing the addition of API that would allow for global additions to context props covering all of these areas. I don’t have a specific API in mind yet, just opening the discussion as I see a need here.
Issue Analytics
- State:
- Created 2 years ago
- Comments:35 (35 by maintainers)
Top Results From Across the Web
A Guide to React Context and useContext() Hook
The main idea of using the context is to allow your components to access some global data and re-render when that global data...
Read more >Using React Global State with Hooks and Context | Savas Labs
Set and manage global state for a small React Native app built from functional components, without using Redux or Mobx.
Read more >useContext Hook - React Hooks Handbook - Design+Code
Manage global states throughout the entire application. ... Inside of this custom hook, we'll be using the useContext hook, that allows us to...
Read more >Hooks API Reference - React
This page describes the APIs for the built-in Hooks in React. ... A component calling useContext will always re-render when the context value...
Read more >React Context Patterns with useContext Hook | by Cheri Hung
React Context API offers a convenient way to implement a global state without adding any other dependencies to your app. It isn't a...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
One cosmetic imperfection with that solution: if you use
initializeContextProps
at the server integration point, then you’ll have to write it with CJS.You’re right, we should dig deeper into enabling the server code to be processed by Vite. In the meantime,
nodemon
+ CJS + aenv.js
is the way to go. Not pretty, but works.Closing in favor of https://github.com/brillout/vite-plugin-ssr/issues/164