Comment utiliser les cookies dans React JS
const [cookies, setCookie, removeCookie] = useCookies(['cookie-name']);
Alert Armadillo
const [cookies, setCookie, removeCookie] = useCookies(['cookie-name']);
import React, { Component } from "react";
import { instanceOf } from "prop-types";
import { withCookies, Cookies } from "react-cookie";
class App extends Component {
static propTypes = {
cookies: instanceOf(Cookies).isRequired
};
state = {
user: this.props.cookies.get("user") || ""
};
handleCookie = () => {
const { cookies } = this.props;
cookies.set("user", "gowtham", { path: "/" }); // setting the cookie
this.setState({ user: cookies.get("user") });
};
render() {
const { user } = this.state;
return (
<div className="App">
<h1>React cookies</h1>
{user && <p>{user}</p>}
<button onClick={this.handleCookie}>Set Cookie</button>
</div>
);
}
}
export default withCookies(App);