How to set Cookie in ReactJS ?
Creating React Application:
Step 1: Create a React application using the following command:
npx create-react-app setcookiedemo
Step 2: After creating your project folder i.e. setcookiedemo, move to it using the following command:
Project Structure: It will look like the following.
App.js: Now write down the following code in the App.js file. Here, App is our default component where we have written our code to set cookies in localStorage with basic UI.
Step to Run Application: Run the application using the following command from the root directory of the project:
The following will be the output if the user enters a username which is set as a cookie as shown below:
Now go to your localStorage of browser by inspecting the browser page, there you can see our cookie which is set with key=’username’ and value=’gouravhammad’ as shown below: