I'm new to React and I'm trying to use Context API. Here is my code:
FormColorContext.js
import React, { useState } from 'react';
export const FormColorContext = React.createContext();
const FormColorProvider = (props) => {
const [color, setColor] = useState('white')
return (
<FormColorContext.Provider value={{color}}>
{props.children}
</FormColorContext.Provider>
)
}
export default FormColorProvider
dashboard.js
import React from 'react';
import FormColorProvider from '../context/FormColorContext';
import DefaultLayout from '../layout/default-layout';
import EmptyDashboardComponent from '../components/Dashboard/EmptyDashboardComponent';
import NewFormComponent from '../components/Dashboard/NewFormComponent';
import ColorSelectorComponent from '../components/Dashboard/ColorSelectorComponent';
import styles from '../styles/dashboard.module.css';
export default function Dashboard() {
return (
<div>
<DefaultLayout>
<div className={styles.containerMain}>
<h1 className={styles.headingCenter}>Create a new form</h1>
<FormColorProvider>
<ColorSelectorComponent/>
<NewFormComponent/>
</FormColorProvider>
</div>
</DefaultLayout>
</div>
)
}
NewFormComponent.js
import React, { useContext } from 'react';
import styles from '../../styles/NewFormComponent.module.css';
import FormColorContext from '../../context/FormColorContext';
export default function NewFormComponent() {
const color = useContext(FormColorContext);
console.log(color);
return (
<div className={styles.formContainer}>
</div>
)
}
I made sure that all imports are correct, but for some reason when I try to log color variable I'm getting undefined. What am I doing wrong? Thanks in advance. Renato
-
what is you're project folder structure?Taghi Khavari– Taghi Khavari2021年01月03日 11:09:57 +00:00Commented Jan 3, 2021 at 11:09
-
my project structure: ibb.co/jztdHqVRenato Maretić– Renato Maretić2021年01月03日 11:12:51 +00:00Commented Jan 3, 2021 at 11:12
3 Answers 3
Sample Example: Expo Snack
In your Context Provider you are passing an object, so access color like below:
NewFormComponent.js:
import React, { useContext } from 'react';
import styles from '../../styles/NewFormComponent.module.css';
import {FormColorContext} from '../../context/FormColorContext';
export default function NewFormComponent() {
const {color} = useContext(FormColorContext);
console.log(color);
return (
<div className={styles.formContainer}>
</div>
)
}
FormColorContext.js:
import React, { useState } from 'react';
export const FormColorContext = React.createContext();
export const FormColorProvider = (props) => {
const [color, setColor] = useState('white')
return (
<FormColorContext.Provider value={{color}}>
{props.children}
</FormColorContext.Provider>
)
}
** dashboard.js: **
import React from 'react';
import {FormColorProvider} from '../context/FormColorContext';
import DefaultLayout from '../layout/default-layout';
import EmptyDashboardComponent from '../components/Dashboard/EmptyDashboardComponent';
import NewFormComponent from '../components/Dashboard/NewFormComponent';
import ColorSelectorComponent from '../components/Dashboard/ColorSelectorComponent';
import styles from '../styles/dashboard.module.css';
export default function Dashboard() {
return (
<div>
<DefaultLayout>
<div className={styles.containerMain}>
<h1 className={styles.headingCenter}>Create a new form</h1>
<FormColorProvider>
<ColorSelectorComponent/>
<NewFormComponent/>
</FormColorProvider>
</div>
</DefaultLayout>
</div>
)
}
3 Comments
undefined as useContext value!The problem is in imports. You import the default value in NewFormComponent.js file. Change the import to import {FormColorContext} from '../../context/FormColorContext';
Moreover, it is a dupliate of useContext() returns undefined you could have googled it)
2 Comments
{ color }. You may write colorContext = useContext(someContext) and access color later in your code by writing colorContext.color.You have a problem in importing the FormColorContext in your NewFormComponent and it's passing the undefined to useContext method and it returns undefined too.
here is what you need to do:
import React, { useContext } from 'react';
import styles from '../../styles/NewFormComponent.module.css';
import FormColorContext from '../context/FormColorContext';
export default function NewFormComponent() {
// remember to destructure the color variable
const { color } = useContext(FormColorContext);
console.log(color);
return (
<div className={styles.formContainer}>
</div>
)
}