npm install react-cookie --save
地址:https://github.com/eXon/react-cookie
Examples
import { Component } from 'react';
import cookie from 'react-cookie';
export default class MyApp extends Component {
constructor(props) {
super(props);
this.state = { userId: cookie.load('userId') };
}
onLogin(userId) {
this.setState({ userId });
cookie.save('userId', userId, { path: '/' });
}
onLogout() {
cookie.remove('userId', { path: '/' });
/** Clear all cookies starting with 'session' (to get all cookies, omit regex argument) */
Object.keys(cookie.select(/^session.*/i)).forEach(name => cookie.remove(name, { path: '/' }))
}
render() {
return (
<LoginPanel onSuccess={this.onLogin.bind(this)} />
);
}
}