This is the final post in a series of posts on React context with TypeScript. In the previous post, we consumed a context in a class component.
This is the final post in a series of posts on React context with TypeScript. In the previous post, we consumed a context in a class component. In this post, we will learn how to create a context without having to pass a default and then do any undefined
checks when consuming it.
The type for createContext
requires a default value to be passed into it, but often it doesn’t make sense to pass a default. So, we end up passing undefined
as the default:
const ThemeContext = React.createContext<
ThemeContextType | undefined
>(undefined);
… and then checking for undefined
everywhere we consume it:
const { theme, setTheme } = useTheme()!;
A quick solution to the problem is to use the not-null assertion operator and remove undefined
from the context type.
const ThemeContext = React.createContext<
ThemeContextType
>(undefined!);
This works nicely because consuming code doesn’t need undefined
checks. However, we are still passing a default.
Article covers: How native is react native?, React Native vs (Ionic, Cordova), Similarities and difference between React Native and Native App Development.
In this article, you will learn what are hooks in React JS? and when to use react hooks? Also, we will see the react hooks example.
As we start learning new technologies we want to start building something or work on a simple project to get a better understanding of the technology.
React provides support for the server-side and frontend. Today, React development services are in demand as more and more organizations are considering software solutions that are crafted from the framework.
In this article we will learn how to Validating React Forms With React-Hook-Form . Validating inputs is very often required. For example, when you want to make sure two passwords inputs are the same, an email input should in fact be an email or that the input is not too long