Reputation: 121
I am trying to import the value of useContext but I am getting this error :
Error: Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: undefined. You likely forgot to export your component from the file it's defined in, or you might have mixed up default and named imports.
What I am missing here ?
AllThemeContext.js
import React, { createContext, useState } from "react";
export const AllContext = createContext();
const AllContextProvider = (props) => {
const submittedInput = props.submittedInput;
return (
<AllContext.Provider value={{ submittedInput }}>
{props.children}
</AllContext.Provider>
);
};
export default AllContextProvider;
logo.js
import React from "react";
import AllContextProvider from "../context/AllThemeContext";
import "./Logo.scss";
// Component
const Logo = () => {
return (
<AllContextProvider.Consumer>
{(context) => {
console.log(context);
}}
</AllContextProvider.Consumer>
);
};
export default Logo;
Upvotes: 0
Views: 2036
Reputation: 38922
You should import AllContext
from AllThemeContext
which is the named export and not the default export.
import React from "react";
import { AllContext } from "../context/AllThemeContext";
import "./Logo.scss";
// Component
const Logo = () => {
return (
<AllContext.Consumer>
{(context) => {
console.log(context);
}}
</AllContext.Consumer>
);
};
export default Logo;
Upvotes: 1
Reputation: 12777
Because you don't return anything in the children of AllContextProvider.Consumer
.
You need add return component:
{(context) => {
console.log(context);
return <div>Hello world</div>
}}
Upvotes: 0