简体   繁体   English

将react-cookie与react redux集成

[英]Integrating react-cookie with react redux

My main application is based on this old boilerplate which I have been slowly updating. 我的主要应用是基于这个旧的样板 ,我一直在慢慢更新。 Currently, all of the dependencies have been updated except for react-cookie. 目前,除react-cookie之外,所有依赖项都已更新。

I am trying to upgrade react-cookie to version 3.0.4 using this tutorial but I need some help overcoming some challenges I am facing during the transition process. 我正在尝试使用本教程将react-cookie升级到3.0.4版,但我需要一些帮助来克服我在转换过程中遇到的一些挑战。

Following the tutorial, I changed index.js to 在教程之后,我将index.js更改为

ReactDOM.render(
  <CookiesProvider>
    <Provider store={store}>
      <App />
    </Provider>
  </CookiesProvider>,
  document.querySelector('.wrapper'));

Now, my app.js file looks like this: 现在,我的app.js文件如下所示:

import React, { Component } from 'react'
import { withCookies } from 'react-cookie'
import Routes from '../Routes'

class App extends Component {
  render() {
    return (
      <div className="container">
        <Routes cookies={this.props.cookies} />
      </div>
    );
  }
}

export default withCookies(App)

Now, my biggest concern comes here. 现在,我最关心的是这里。 My Routes component was never meant to be a Redux container so I changed it to this to accommodate the tutorial: My Routes组件从未打算成为Redux容器,因此我将其更改为此以适应教程:

import React from 'react'
import { connect } from 'react-redux'
import { BrowserRouter, Route, Switch } from 'react-router-dom'
import ScrollUpButton from 'react-scroll-up-button'

// Import miscellaneous routes and other requirements
import NotFoundPage from './components/pages/not-found-page'

// Import Header and footer
import HeaderTemplate from './components/template/header'
import FooterTemplate from './components/template/footer'

// Import static pages
import HomePage from './components/pages/home-page'

// Import authentication related pages
import Register from './components/auth/register'
import Login from './components/auth/login'
import Logout from './components/auth/logout'
import ForgotPassword from './components/auth/forgot_password'
import ResetPassword from './components/auth/reset_password'
import ConfirmationMessage from './components/auth/confirmation_message'
import ResendVerificationEmail from './components/auth/resend_verification_email'

// Import dashboard pages
import Dashboard from './components/dashboard/dashboard'
import ChangePassword from './components/dashboard/profile/change-password'

// Import simulator pages
import Simulator from './components/simulator/index'

// Import higher order components
import RequireAuth from './components/auth/require_auth'

const BrowserRoutes = () => (
  <BrowserRouter>
    <div>
      <HeaderTemplate logo="Stress Path Simulator" />
      <Switch>
        <Route exact path="/" component={HomePage} />
        <Route exact path="/register" component={Register} />
        <Route exact path="/login" component={Login} />
        <Route exact path="/logout" component={Logout} />
        <Route exact path="/forgot-password" component={ForgotPassword} />
        <Route exact path="/reset-password/:resetToken" component={ResetPassword} />
        <Route exact path="/confirmation-message"  render={() => <ConfirmationMessage message="Please click on the link we sent to your email to verify your account." /> } />
        <Route exact path="/resend-verification-email" component={ResendVerificationEmail} />
        <Route exact path="/profile/change-password" component={RequireAuth(ChangePassword)} />
        <Route exact path="/confirmation-password-changed" render={() => RequireAuth(<ConfirmationMessage message="Password has been successfully changed!" />)} />
        <Route exact path="/simulator" component={RequireAuth(Simulator)} />
        <Route exact path="/dashboard" component={RequireAuth(Dashboard)} />
        <Route component={NotFoundPage} />
      </Switch>
      <FooterTemplate />
      <ScrollUpButton />
    </div>
  </BrowserRouter>
);

const mapStateToProps = (state, ownProps) => {
  return ({
    state: state,
    cookies: ownProps.cookies
  });
}

export const Routes = connect(mapStateToProps, null)(BrowserRoutes)

export default Routes

I believe the problem essentially arises here. 我认为问题基本上出现在这里。 By doing so, I thought I would have been able to use the cookies from every single component like this: 通过这样做,我以为我能够使用每个组件的cookie,如下所示:

//get this.props.cookies
const { cookies } = this.props;

//setting a cookie
cookies.set('name', 'Ross', { path: '/' });

//getting a cookie
cookies.get('name');

However, that doesn't seem the case and I cannot get cookies to work in any of my components especially in my actions/auth.js. 但是,情况似乎并非如此,我无法在任何组件中使用cookie,特别是在我的actions / auth.js中。 Does anyone have any suggestions? 有没有人有什么建议? How can I efficiently use cookies in this scenario? 如何在这种情况下有效地使用cookie? I am assuming I can send down the cookies prop to each component that needs it but I am curious to find out what could be the best/cleanest way of using react-cookie with redux. 我假设我可以将cookie支持发送到需要它的每个组件,但我很想知道什么是使用reactx与redux的最佳/最干净的方法。 I am fairly new to the MERN JavaScript software stack and mostly self-thought so I am a bit confused about some concepts. 我对MERN JavaScript软件堆栈相当新,而且大多是自我思考的,所以我对一些概念感到有些困惑。 For example, if in Routes I am saving cookies into the redux's store, how can I access those cookies afterwards? 例如,如果在路由中我将cookie保存到redux的商店,那我怎么能在之后访问这些cookie?

Instead of passing the cookies from the App/Router down, it is better to wrap only the components that will need the cookies. 不是从应用程序/路由器传递cookie,而是最好只包装需要cookie的组件。 For example your Login component would look like this: 例如,您的Login组件将如下所示:

class Login extends React.Component {
   render() {
      let { cookies } = this.props;
      let useCookie = cookies.get("testCookie");
      ...
   }
}

export default withCookies(connect(mapStateToProps, null)(Login));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM