0

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

asked Jan 3, 2021 at 11:01
2
  • what is you're project folder structure? Commented Jan 3, 2021 at 11:09
  • my project structure: ibb.co/jztdHqV Commented Jan 3, 2021 at 11:12

3 Answers 3

2

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>
 )
}
answered Jan 3, 2021 at 11:05
Sign up to request clarification or add additional context in comments.

3 Comments

he's getting undefined as useContext value!
after adding { color } I'm getting TypeError: Cannot destructure property 'color' of 'Object(...)(...)' as it is undefined.
I made it work, error was combination of missing { color } and missing { } on import because it wasn't default export. Thank you for your help.
1

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)

answered Jan 3, 2021 at 11:13

2 Comments

I made it work, error was combination of missing { color } and missing { } on import because it wasn't default export. Thank you for your help.
You are welcome) But you don't actually need to destructure context as { color }. You may write colorContext = useContext(someContext) and access color later in your code by writing colorContext.color.
0

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>
 )
}
answered Jan 3, 2021 at 11:17

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.