带钩的React Cookie

时间:2019-06-06 16:21:59

标签: reactjs cookies redux-thunk react-cookie

我正在使用React-cookie,redux,redux-thunk和钩子。

我不知道如何将“令牌”的值存储为cookie。

这是组件let stuff = [{"id": "48202847","name": "Doe"},{"id": "17508","name": "Marie"},{"id": "175796","name": "Robert"},{"id": "175796","name": "Ronald"}]; let result = [...new Set(stuff.map(x => x.name[0]))] .reduce((acc, val) => { return acc = { ...acc, [val]: stuff.filter(x => x.name.startsWith(val)) } }, {}); console.log(result);

App.js

实际上<Provider store={store}> <CookiesProvider> <BrowserRouter> <Switch> <Route exact path="/release/:id" component={Release} render={() => (<Login cookies={this.props.cookies} />)} /> <Route exact path="/login" render={() => (<Login cookies={this.props.cookies} />)} component={Login} /> </Switch> </BrowserRouter> </CookiesProvider> </Provider> 组件是作为钩子制作的

由于此组件的调用,我收到了令牌的值

Login

如何将值function Form({ handleSubmit, login }, props) { const [token, setToken] = useState(undefined); const onSubmit = (user) => { login(user); }; return ( <form onSubmit={handleSubmit(onSubmit)} className={styles.flexColumn}> <div className={styles.username}> <P>username</P> <Field name="username" component="input" type="text" className={styles.input} /> </div> <div className={styles.password}> <P>password</P> <Field name="password" component="input" type="text" className={styles.input} /> </div> <div className={styles.downSection}> <Flex> <div> <P> Serve Aiuto? </P> </div> <a href="#"> <div className={styles.contactLink}> <P>Contattaci</P> </div> </a> </Flex> <Button type="submit" text="Accedi" /> </div> </form> ); } Form.propTypes = { handleSubmit: PropTypes.func.isRequired, login: PropTypes.func.isRequired, }; const mapStateToProps = (state, ownProps) => ({ cookies: ownProps.cookies, }, console.log(ownProps)); const mapDispatchToProps = { login: loginAction, }; const enhance = compose( connect(mapStateToProps, mapDispatchToProps), reduxForm({ form: 'login' }), ); export default enhance(Form); 存储为cookie?感谢token,我得到了这个价值 我必须使用库react-cookies。

谢谢。

1 个答案:

答案 0 :(得分:1)

为此使用图书馆... 例如react-cookie

您可以轻松地做到:

import {useCookie} from 'react-cookie'

并使用cookie。

看看here