useContext react
import React from 'react'
import ReactDOM from 'react-dom'
import { useContext } from 'react'
const konteks = React.createContext(null);
function components(){
return(
<konteks.Provider value="hello world">
<MyComponent />
</konteks.Provider>
);
}
function MyComponent(){
const getcontext = useContext(konteks);
return(
<div>{getcontext}</div>
);
}
const body = ReactDOM.createRoot(document.getElementById("body"));
body.render(<MyComponent/>);
I already know the use of useContext, where we can pass data to other components,But I'm still confused regarding the implementation and the code above doesn't work, where is the error?
2 Replies
konteks
should be Konteks
(capitalize first letter): https://react.dev/reference/react/createContext#providercreateContext – React
The library for web and native user interfaces
Thanks for that sir