React Context with TypeScript
Let me show you that you can manage states on React without any additional library like Redux or MobX. It is simple and easy to learn.
Some years ago and even now, we found that some React developers included a state management library. The popular one is Redux. Some even started app development already with Redux installed. So, it is unnecessary now. React can build a complex app without any additional state management library like Redux or MobX. You can visit this page, https://reactjs.org/docs/faq-state.html#should-i-use-a-state-management-library-like-redux-or-mobx. It confirmed the statement.
React should not get started with an additional library to manage states. That is also not beginner-friendly because we need to learn how to use the additional one. Some said it is overkill, and I think that it will make the code more complex. It is better to get an approach with less effort and few libraries.
I also recommend moving to React Hook for better code if you are still using React Component. You can visit https://reactjs.org/docs/hooks-intro.html for more information about React Hook.
There are many examples of React Context out there on JavaScript. Here I implemented it to the previous Image Service that I made on TypeScript. I also show you how to test it.
createContext
|
|
First, we need to create a context. Here is the upload page context with a list and two setters.
Provider
type Props = {
title: string
description: string
}
export default function UploadPage(props: Props) {
let [downloadList] = useState<DownloadButtonProps[]>([])
let [jpeg, setJPEG] = useState('');
let [webp, setWebP] = useState('');
return (
<UploadPageContext.Provider value={{downloadList, setJPEG, setWebP}}>
<div className="container mx-auto max-w-md my-8">
<Title value={props.title} />
<Description value={props.description} />
<UploadForm />
<DownloadList downloadList={downloadList} />
</div>
<ImageComparison jpeg={jpeg} webp={webp} />
<Copyright />
</UploadPageContext.Provider>
)
}
In the upload page, we created 3 states; download list, JPEG, and WebP. We need setters for JPEG and WebP. But we didn't need that for the download list. Then we called the UploadPageContext.Provider
. We put it on the root of the return and set the values.
useContext
export default function File() {
let uploadPageContext = useContext(UploadPageContext);
let handler = (file: any) => {
if (file) {
generateImages(file, uploadPageContext.setJPEG, uploadPageContext.setWebP, uploadPageContext.downloadList)
}
}
return (
<FileUploader handleChange={handler} types={fileTypes} maxSize={16} />
)
}
At the bottom level, we called those states, a list, and two setters, with useContext
, then used them. Above, I called them when generating images.
Test
|
|
Because I worked with Atomic Design, I only needed to test the atom File.tsx
with File.test.tsx
. I renamed the File
function to _File
to avoid a conflict. I covered the _File
with UploadPageContext.Provider
and put dummy setters there.
Related Articles
- Convert Any Images to JPEG and WebP, Compress, and Resize on the Client Side
- React with TypeScript
- Next.js vs Hugo
- Server-Side Rendering with Next.js and TypeScript
- Atomic Design with React