I'm attempting to set a default value through a context (which I'll later update). However I can't seem to obtain the value in the consumer.
I have a SummaryContext
file which contains two default values title
and summary
. This is used in Body.js
and the header
value passed on. And finally a Title
file which needs to render one of the values.
The line console.log("TITLE.JS: " + {title});
in Title.js
prints TITLE.JS: [object Object
to the console.
I'm at a loss as to what I've got wrong.
SummaryContext.js
import { createContext } from 'react'
const header = {
title: "sdlfkjasdf",
summary: "kmkmkm"
}
export const SummaryContext = createContext(header);
Body
The line <WikiHeader \>
contains a reference to the file Title.js
which is the file I'm attempting to pass these values to.
import React, {useState} from "react";
import WikiHeader from './wikiheader/WikiHeader'
import {SummaryContext} from "../contexts/SummaryContext"
function Body() {
const [header, setHeader] = useState(SummaryContext);
return (
<>
<SummaryContext.Provider value={{header}}>
<GoogleAd />
<WikiHeader />
</SummaryContext.Provider>
</>
);
}
export default Body;
Title.js
import React, {useContext} from "react"
import {SummaryContext} from "../../../contexts/SummaryContext"
import '../../../App.css'
export function Title() {
const {title, summary} = useContext(SummaryContext);
console.log("TITLE.JS: " + {title});
return (
<div className="container">
<div className="detailTitle"><span><h3>The title: {title}</h3></span></div>
</div>
);
}