javascript - 轻松设置 react redux with next.js (React)

标签 javascript reactjs redux react-redux next.js

我看过使用 redux 的 next.js 示例。但是有什么简单的方法可以用 next.js 设置 redux 吗? 我想设置 redux 我以前用 react.js 设置的方式

最佳答案

假设您了解 Redux 和 React。

首先,安装这些包

$ npm install next-redux-wrapper react-redux redux redux-thunk --save

$ npm install redux-devtools-extension --save-dev


覆盖或创建默认App,创建文件./pages/_app.js如下图:

import withRedux from 'next-redux-wrapper'
import { Provider } from 'react-redux'
import { withRouter } from 'next/router'
import App from 'next/app'

import createStore from 'store/createStore'

class MyApp extends App {
  render () {
    const { Component, pageProps, router, store } = this.props
    return (
          <Provider store={store}>
              <Component router={router} {...pageProps} />
          </Provider>
    )
  }
}

export default withRedux(createStore)(
  withRouter(MyApp)
)

在您的页面/组件中,您可以像往常一样使用 Redux。

关于javascript - 轻松设置 react redux with next.js (React),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65421030/

相关文章:

javascript - 如何在react js中成功登录后重定向

reactjs - 在每次测试之前手动修改initialState并将其传递给商店?

javascript - 在非结构化对象数组中查找不同键的数量

javascript - 使用 mix it up 在页面加载时过滤 "premium"选项

javascript - 如何在我的 aspx 上调用带有条件更新面板的自定义控件 javascript?

android - 如何在 React Native 中构建调试 apk

javascript - 如何用 Jest 测试 ReactDOM.render 在被条件包装时被调用?

javascript - 使用 React 和 api 调用更新状态的无限滚动

javascript - Redux mapDispatchToProps 作为对象参数不起作用,也没有在开发工具中显示任何预期状态

javascript - 如何从商店本身获取在商店上调度的最后一个操作类型?