Managing cookies in ReactJS can be done using the `react-cookie` library.
Here are the steps:
1. Install the `react-cookie` library in your project by running the command
\`\`\`bash npm install react-cookie —save \`\`\` Or if you’re using yarn: \`\`\`bash yarn add react-cookie \`\`\`1. Import the `react-cookie` library into your component:
\`\`\`js import { useCookies } from ‘react-cookie’; \`\`\`1. For setting a cookie:
\`\`\`jsx const [cookies, setCookie] = useCookies([‘user’]); // useCookies hook setCookie(‘user’, ‘John Doe’, { path: ‘/’ }); \`\`\` In the above code, ‘user’ is the name of the cookie, ‘John Doe’ is the value, and path is an options object (optional) that sets the cookie path.1. For reading a cookie:
\`\`\`jsx const [cookies] = useCookies([‘user’]); // useCookies hook console.log(cookies.user); \`\`\` In the above code, ‘user’ is the name of the cookie.1. For deleting a cookie:
\`\`\`jsx const [cookies, setCookie, removeCookie] = useCookies([‘user’]); // useCookies hook removeCookie(‘user’); \`\`\` In the above code, ‘user’ is the name of the cookie.Note: You’ll need to ensure that your component is wrapped with `
```
import { CookiesProvider } from ‘react-cookie’;
function App() {
return (
}
```
You can find more options to set like Expires, Domain, Secure etc. when setting a cookie, by exploring the `react-cookie` library’s documentation.