0

我正在使用 React-cookies、redux、redux-thunk 和 hooks。

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

这是组件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);

我如何将值存储token为 cookie?由于loginAction 我必须使用库 react-cookies,我得到了这个值。

谢谢。

4

1 回答 1

1

为它使用一个库...例如react-cookie.

你可以简单地做:

import {useCookie} from 'react-cookie'

并使用cookies。

看看这里

于 2020-06-08T05:58:02.450 回答