3,601 questions
-1
votes
1
answer
36
views
Is there a supported way in Next.js App Router to fetch user data on the server, pass it into global UserProvider, and still keep static pages static?
My app structure is:
A root layout (app/layout.tsx)
A shared Header component, rendered on all pages (including static pages like home/about)
Inside the header, an AuthButtons component that should ...
Best practices
1
vote
3
replies
141
views
How to properly manage authentication with Axios and React Context in a TypeScript app?
I’m building a React + TypeScript app and using Axios for API requests. I have an AuthProvider using React Context to manage the user and token, and I’m running into a few questions and issues:
Do I ...
Advice
0
votes
4
replies
92
views
How to access React context for base URL when creating Redux-Toolkit Query API slice?
I am trying to move to RTK Query, but am struggling with one thing - how to access React Context.
I currently am using custom hooks to query a back end. Depending on the environment (dev, uat, prod), ...
2
votes
1
answer
111
views
React - useContext - code executed 2 times in provider
This is my structure about code:
src
App.js
pages
ConfigurationPage.jsx
provider
ConfigurationProvider.jsx
context
ConfigContext.jsx
Explain bug:
The code is executed 2 times in provider.
The ...
0
votes
1
answer
81
views
useContext in TypeScript
import { createContext, useContext, useState, useEffect } from 'react';
import { Appearance } from 'react-native';
import { lightTheme, darkTheme } from '../utility/theme';
type ThemeType = typeof ...
0
votes
0
answers
89
views
Sharing context across apps/packages in Turborepo
My turborepo has several packages, one of which is a ui library that uses some context providers. I export these context providers (Theme, Toast, etc.) so that I can use my UI components in my apps ...
0
votes
0
answers
54
views
Auth context automatically redirect to homepage in case server returns error
if I login with wrong username or password the server (NextJS) will return error message with status code 401, what should happen in this case is that !res.success will be executed and it's indeed ...
0
votes
1
answer
95
views
Context data not updating after navigation to route using same param but different component
I'm building a React app with React Router and a context provider that loads property data based on a propertyId route param.
In my App.jsx, I have the following routes:
<Route path="/edit/:...
0
votes
1
answer
100
views
Does the react context API not hide dependencies?
When I make a component that relies on something like the currently chosen language for example, or maybe a special query function to fetch data, and I provide either of these things via a ...
1
vote
1
answer
76
views
Why do I state variables that are also context variables not render when changed with the useState setter function?
In my Expo React Native application, I have a context variable (trackingAsked) that is also a state variable that I use to display a different opening screen. Everything works fine. On power up, the ...
0
votes
1
answer
58
views
useContext does not show/update the value
I need to save the ID of a field from the settings.tsx view. That value need to be used with other view, like stations.tsx, alarms.tsx, map.tsx.
If later, I need to watch another field, I can change ...
0
votes
1
answer
55
views
Problem using useContext with different views/pages
I am a novice and I need to use a parameter for all view/page of my React Native App.
Scenario: from my setting.tsx view, I can select a field as a preference. From the stations.tsx view, I use ...
1
vote
1
answer
81
views
If a child component consumers a context provider, do its parents also re-render?
Although the docs for context say "React automatically re-renders all the children that use a particular context starting from the provider that receives a different value.", from my testing ...
-1
votes
1
answer
64
views
Why to write .Provider in contexts? [duplicate]
Why do I need to write .Provider?
It works perfectly both with and without it.
Code below works:
import { useState, useEffect, createContext, useContext } from "react";
const ThemeContext = ...
2
votes
1
answer
80
views
setState function not recognized as function
In my React Native app, I'm having a problem where the setState function is not being recognized as a function.
I already inserted the context provider on the App root page and exported the context ...
0
votes
1
answer
81
views
Is posible to create a context in React declaring only one node?
Basically what title says. I want to know if there is any package or other way to declare a context to get data from it (I don´t need to modify that data) without having to declare a provider, a type ...
1
vote
1
answer
91
views
State updates causing excessive re-renders in complex React dashboard with nested components [duplicate]
I'm working on a complex analytics dashboard built with React and TypeScript, which involves multiple nested components. I'm using a combination of state management techniques including React Context, ...
4
votes
1
answer
375
views
React context: Should I use the .Provider property?
Looking at the React documentation for Context, the context is used without the .Provider property:
import { LevelContext } from './LevelContext.js';
/*
LevelContext is defined as
export const ...
-1
votes
1
answer
52
views
useContext data is not updated with latest state when changing routes
I created an auth context and set the object to an initial state, then after wrapping the App() with the provider, I am trying to modify the authContext in 1 (Home.js) component and then try to access ...
3
votes
2
answers
67
views
Why App component is not getting context update
I'm trying to set up a context from a custom hook. Here's my code:
usePlayerInterface.jsx
import { useState } from 'react';
export function usePlayerInterface() {
const [userAction, setUserAction] =...
0
votes
1
answer
112
views
React get data from children context inside the parent component
I'm trying to set up a form with many rows of data, with each individual items. When I click a button, I want to get all of the data from each children, and submit the form.
I'm using context to ...
-1
votes
2
answers
228
views
Why is the state within a react context always the initial state
Original Question
I am trying to learn more about the inner workings of React, and here specifically Context Providers so I don't have a use-case in mind, just trying to understand why it works the ...
0
votes
0
answers
61
views
How to rerender only if the correct part of context state changes?
I have setup a state in the context using a reducer
const playerStateReducer = ((state: PlayerState, action: PlayerAction) => {
switch (action.type) {
case "SET_LOCATION": {
...
0
votes
0
answers
79
views
Where is the context state set in React Context?
I've created an app that stores user information in firebase.
The main profile page consumes the data via a hook that retrieves the data from firebase/storage. What I'm trying to do in this page is ...
0
votes
1
answer
57
views
React useContext State Updated Not Triggering useEffect [duplicate]
I am attempting to implement the React useContext hook to share the state of a user license. I've found this pattern online but can't seem to get it to work. If the license is not valid, the user is ...
0
votes
1
answer
132
views
Fetch user data once loaded Next.js
I'm attempting to fetch the data from user once the page loads.
Although, currently when there is any route changes, it triggers my useEffect and calls the fetch user endpoint which causes my loading ...
1
vote
0
answers
50
views
How to Share a Provider Across Isolated Components?
I'm working on a framework that allows developers to create extensions for a main app. Extensions can add components to different parts of the app layout, such as the sidebar or workspace, ...
-1
votes
1
answer
64
views
React is not updating the Context and the Provider
I have this handler and function to update my react context but it's not updating. It does not dispatch the action
This function switches accounts
const loadAccountSwitch = useCallback(
async (...
0
votes
1
answer
37
views
React useContext method
I created a NewsContext for my React project for accessting static data:
NewsContext:
"use client";
import { createContext, useContext, useState, useEffect } from "react";
export ...
-1
votes
3
answers
169
views
LocalStorage is not working with React Context
I have tried to use useEffect to store user details in local storage but unable. Please do any help on it.
Full Code :
const AuthContext = createContext()
const AuthProvider = ({ children }) => {...
0
votes
1
answer
115
views
context resets to the initial value in the production environment
Description
I encountered a problem while using the Next.js App Router (version 14.2.6).
The issue occurs only in the production environment. it works perfectly fine in the local environment.
I'm ...
0
votes
0
answers
111
views
Electron-Vite-React - Persistent context accross routes
I have an Electron-Vite React (TSX) application. I am pretty new to coding Electron-Vite-React; perhaps my problem is because I am not understanding all the subtilities of certains concepts...
I have ...
0
votes
1
answer
31
views
Force download doesn't work in ContextProvider [duplicate]
I am using Next.js (which is React based framework).
I want to force download some files, so I created custom context for that.
"use client";
import { createContext, useContext, useRef } ...
0
votes
1
answer
59
views
React useContext(): Objects are not valid as a React child when trying to execute function in useContext() api to fetch data
I want to fetch the data by running the API in the useContext() api, basically this API gives me the number of items added in the cart and I need that number only which Im using it in Cart.js ...
0
votes
1
answer
84
views
useContext returns undefined even though it is nested in the provider
I am trying to avoid prop drilling in my Solid application, but contexts all return undefined or my check throws an error.
SettingsContext.tsx
import {
createContext,
useContext,
createSignal,
...
-3
votes
1
answer
59
views
Why do I need a fancy jsx syntax for React Contexts [closed]
I've been out of the frontend js scene for a while, and now try to catch up on React etc.
I don't really get contexts. First you need to create a context somewhere, then you can use <Context....
0
votes
0
answers
47
views
React Context is not changing
I have created a context and I want to change it when I click in SingleProduct component button but it doesn't work
I have ctreated my context in a .ts file DataModalContext.ts:
import { createContext ...
1
vote
1
answer
36
views
Why attendence Context fetching again on navigation back to the teacherattend component?
I need to use attendance data often on my website, So I make it as utility as below:
import React, {
useState,
useEffect,
useMemo,
useCallback,
createContext
} from 'react';
import { ...
1
vote
1
answer
255
views
How to propagate and clear thread context across JVMs for dynamic debug logging
I am trying to implement dynamic debug logging by populating a ThreadContext with specific attributes and filtering logs based on those attributes.
Here’s what I have achieved so far:
Within the same ...
2
votes
2
answers
81
views
Context Provider not passing value to children
I am trying to create a parent/overarching context. The purpose of the context is to store UserInfo which should then be available to all children.
UserInfo is set by getting config from an API.
...
1
vote
2
answers
2k
views
Child Component Re-renders Due to useFormContext in React Hook Form
I'm experiencing a challenge with React Hook Form where my child component InputX re-renders every time there's an update in the parent component or other parts of the application, despite no changes ...
3
votes
0
answers
464
views
How to resolve hydration issue in Nextjs 15 with useOptimistic in my context utilizing React 19?
Playing with a store in Next.js 15 and using React 19 I'm running into an issue with my cart context and keep receiving a hydration error of:
An optimistic state update occurred outside a transition ...
-1
votes
1
answer
43
views
Load React Modal with out refreshing parent component
I have been trying to understand how to populate my modal with asynchronous data and not cause the parent page to rerender. I have tried making a separate context for the modal, moving modal out of ...
-1
votes
1
answer
48
views
React Context provider with TypeScript Error
I'm trying to write and context to set an id in local storage . Here's my code
import React, {
createContext,
useEffect,
useMemo,
useState,
ReactNode,
} from 'react';
// Interface pour le ...
1
vote
2
answers
38
views
Uncaught Error: useNatureRecord must be used within a NatureRecordProvider in React Native TypeScript app
I'm developing a React Native app with TypeScript and using Firebase for data storage. In my app, I have a context provider (NatureRecordProvider) that should wrap two main screens, NatureRecordForm ...
0
votes
4
answers
116
views
Is there any way to initialize a context with data from an api?
I'm trying to create a context whose default value will come from an external source. I would need to do something similar to doing the fetch outside of an asynchronous function (I don't think this is ...
0
votes
1
answer
239
views
Setting up ContextProvider with Expo Stack
I have a RootStack that I'm trying to wrap so I have access to my user object(s) and it's not updating like it did when I had it bare. Is this wrong?
main _layout file
import { useEffect } from '...
1
vote
2
answers
166
views
How to implement 'useContext' in Next JS with .tsx files?
I am trying to implement dual theme (dark & light) in Next JS using MUI using use context method.
It gives me this error:
⨯ src\app\page.tsx (6:50) @ useThemeContext
⨯ TypeError: (0 , ...
0
votes
1
answer
97
views
React UseContext not updating state
I am attempting to update state using a context provider. I cannot figure out where I am going wrong.
The state simply doesn't update. If anyone could point me in the right direction or suggest ...
0
votes
1
answer
136
views
How to assign complex objects to context in React?
I am trying to set data of context.
context :
import React from "react";
const AppContext = React.createContext<IContext>({} as IContext);
export default AppContext;
In App.tsx file I ...