I am trying to pass data from a JSON file to a component with useContext:
import sessionData from '../session.json';
import React from 'react';
export const SessionContext = React.createContext(sessionData);
export const SessionProvider = SessionContext.Provider;
export const SessionConsumer = SessionContext.Consumer;
console.log('session data', sessionData); //console log ok
Next I am passing it into a class component
static contextType = SessionContext;
componentDidMount() {
const sessId = this.context;
console.log('steps', sessId); // log ok
}
render() {
return <SessionProvider value={sessId}> //value returns undefined.
<Child A />
<Child B />
<Child C />
</SessionProvider>;
}
In the first two code blocks shown, my data returns fine. Now I want to pass it to the child components (which are functional component). However, when I try to pass a value to the session provider <SessionProvider value={sessId}
, the value returns as undefined.
I've seen other examples where the value is passed directly into the component like ``{sessId.name}. However, if I'm just trying to make
sessId``` available to all the child components.