
import React, { createContext } from "react";
const UserContext = createContext();
const ConsumerProvider = ({ children }) => {
const [name, setName] = useState("John Doe");
const [age, setAge] = useState(1);
const happyBirthday = () => setAge(age + 1);
return (
<UserContext.Provider value={{ name, age, happyBirthday }}>
{children}
</UserContext.Provider>
);
};
const withConsumer = (Child) => (props) => (
<UserContext.Consumer>
{(context) => <Child {...props} {...context} />}
{/* Another option is: {context => <Child {...props} context={context}/>}*/}
</UserContext.Consumer>
);
export { ConsumerProvider, withConsumer };
<ConsumerProvider>
<Component {...pageProps} />
</ConsumerProvider>
나 해당 컴포넌트를 export 할때 다음과 같이 사용 할 수 있다.
export default withConsumer(Landing);