Type inference error when creating a React Context

Advertisements I’m trying to create simple context provider in a project that uses TypeScript, typed as such: import { createContext } from ‘react’; export type ContextType = { foo: boolean, }; export const FooContext: React.Context<ContextType> = createContext({ foo: true, }); It looks simple enough, but on FooContext TypeScript is telling me that Type ‘Context<{ loading:… Read More Type inference error when creating a React Context

Button component doesn't work with useContext hook

Advertisements I am practicing useContext hook with Next.js if it matters and created a small site that changes background color of <Paragraph></Paragraph> element for printing text and <Button></Button> element to change theme. I organized it with <Layout /> where button and paragraph are placed. As written in tutorial, I can place this theme change logic… Read More Button component doesn't work with useContext hook

How to avoid "setState never used" when using useContext hook

Advertisements I know that if you have something like this… const [state, setState] = useState(‘some state’); and you are getting the warning for setState is never used, you should simply not useState, and instead, a normal variable. My question is, what if this is useContext instead of useState as shown below? I have this structure… Read More How to avoid "setState never used" when using useContext hook

TypeScript React createContext() default value

Advertisements I been kinda stumped for a couple hours trying to figure out what to set as my default value on my createContext function. This is my code. // PetTypeProvider.tsx import { useState, createContext, useContext } from ‘react’; const PetTypeContext = createContext(”); const UpdatePetTypeContext = createContext((event:React.MouseEvent<HTMLElement>) => event); export function usePetType() { return useContext(PetTypeContext) }… Read More TypeScript React createContext() default value

How to implement jsx implementation of react context in tsx

Advertisements I’m using the second answer of this post to implement a React context that can be updated from a child component. This is my code so far: app/authContext/index.tsx import React, { useState } from "react"; export const AuthContext = React.createContext({ auth: {user: null}, setAuth: () => {} }) export const AuthContextProvider = (props: any)… Read More How to implement jsx implementation of react context in tsx