Starting with ReactJS and NextJS
I have the following Layout component and using context to set values from the children component
export interface AuthContextModel {
title: string;
description: string;
showSignup: boolean;
}
export const AuthContext = createContext(null);
export const AuthLayout = ({children}) => {
const [authContext, setAuthContext] = useState<AuthContextModel>();
return (
<>
<Head>
<title>Authentication</title>
<HeadComponent/>
</Head>
<AuthContext.Provider value={setAuthContext}>
<h4>{authContext?.title}</h4>
<p className="text-muted mb-4">Sign in to continue to Chatvia.</p>
{children}
</AuthContext.Provider>
</>
)
}
And the Login
page extends the Layout
export default function Login(props) {
const setAuthContext = useContext(AuthContext);
useEffect(() => {
setAuthContext({
title: 'Sign In'
})
}, [])
return (
<AuthLayout>
<form onSubmit={handleSubmit}>
...
</form>
</AuthLayout>
)
}
But the setAuthContext
in the Login
function is giving the following error
TypeError: setAuthContext is not a function
How can I update the context variable from the children component?