How to return 404 status code in ReactJS?
Asked Answered
T

2

6

How can I return 404 status code in React (with react-router-dom)?

<Route component={NotFoundPage} path="*" />

The answers I got can return a 404 page, but not the http status code. Is there any way around that?

Trunk answered 16/7, 2020 at 15:8 Comment(0)
B
4

React runs on the client. Under normal circumstances, you can't generate a 404 error response because there is no request or response at all.

If you were using some server-side renderer (such as Next.js) then it would likely provide a method for generating a 404 error, but it would be specific to that SSR framework.

Ber answered 16/7, 2020 at 15:40 Comment(0)
C
1

Your Js or any client side service doesnt return response with code. You can get 404 status code from server. In front end there is no way to send status code. Status code is sent by server depending on the request sent by Client side app.

Coastwise answered 16/7, 2020 at 15:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.