+ 3

How do i set axios error message-box background color-red in react js?

function NetworkError(props) { return ( <div style={{ backgroundColor: 'red', padding: '1rem' }}> <h2>Network Error</h2> <p>{props.message}</p> </div> ); }

7th Apr 2023, 7:09 AM
Hinal Goswami
Hinal Goswami - avatar
2 Answers
+ 4
Wrap your Axios request in a try-catch block to handle errors: try { const response = await axios.get('your-api-endpoint'); // handle successful response } catch (error) { return <NetworkError message={error.message} />; } function NetworkError(props) { const { message } = props; return ( <div style={{ backgroundColor: message.includes('Network Error') ? 'red' : 'white', padding: '1rem' }}> <h2>Network Error</h2> <p>{message}</p> </div> ); }
7th Apr 2023, 3:12 PM
Dev Nasfam
Dev Nasfam - avatar
+ 3
Where is your attempt?
7th Apr 2023, 8:39 AM
Sakshi [Offline 🙃]
Sakshi [Offline 🙃] - avatar