

Creating a message component for displaying error in ReactJS
source link: https://dev.to/kitlung/creating-a-message-component-for-displaying-error-in-reactjs-5cm4
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Creating a message component for displaying error in ReactJS
Apr 10
・2 min read
Hi,
Today I would like to share my way of displaying error or custom messages in React.
The idea is to use context and custom hook so we are able to access the component on all pages.
Custom Message Context Provider
Let's create a context provider first.
// CustomMessageProvider.tsx
import { createContext, useState } from 'react';
/*
* show : whether show or hide the message block
* type : what theme is the message block
* msg : message displaying on the message block
*
* You can add more field if you want
*/
interface Msg {
show: boolean;
type: 'error' | 'info' | 'success' | 'warning';
msg: string;
}
const defaultMsg = {
show: false,
type: 'success',
msg: ''
} as Msg
export const CustomMessageContext = createContext({
msg: initMsg as Msg,
setMsg: (_: Msg) => {}
})
const CustomMessageProvider = ({children}) => {
const [msgState, setMsgState] = useState(initMsg)
// CustomMessageContext contains an object
// and also a method for setState
const contextValue = {
msg: msgState,
setMsg: (val: Msg) => setMsgState(val)
}
return (
<CustomMessageContext.Provider value={contextValue}>
{children}
</CustomMessageContext.Provider>
);
}
Then we can add the provider to App.tsx
so that we can access the context on all pages.
// App.tsx
const App = () => {
<CustomMessageProvider>
...
</CustomMessageProvider>
}
export default App
Great!
Custom hook
What we are going to do now is to add a custom hook so we don't have to call useContext in all pages
// customMessageHook.tsx
const useMsg = () => {
const { msg, setMsg } = useContext(CustomMessageContext);
return { msg, setMsg };
}
export default useMsg;
Integration
We can now call useMsg on all pages.
Let say you want to display an error if the fetch API failed on a page.
const { setMsg } = useMsg();
...
const fetchApi = async () => {
try {
...
} catch (err) {
setMsg({
show: true,
type: 'error',
msg: 'Oops, error'
})
}
}
Ending
I know there are some more elegant ways to implement this, feel free to leave me a comment and share what you think.
Have a nice day.
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK