question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Suggestion: global addContextProps hook

See original GitHub issue

I 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 run dotenv.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:closed
  • Created 2 years ago
  • Comments:35 (35 by maintainers)

github_iconTop GitHub Comments

1reaction
brilloutcommented, Apr 1, 2021

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 + a env.js is the way to go. Not pretty, but works.

// env.js

const env = {}

if( typeof window !== 'undefined' ){
  window.env = env
} else {
  // Node.js
  global.env = env
}
import './env' // for page code
require('./env') // for server code

env.someGlobalVariable = 'I am available to everyone';
0reactions
brilloutcommented, Oct 7, 2021
Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found