简体   繁体   English

createProvider 不是从 react-redux 导出的吗?

[英]createProvider is not exported from react-redux?

I am trying to create multiple distinct Redux stores, for that am using createProvider() method in 'react-redux'.我正在尝试创建多个不同的 Redux 存储,为此我在“react-redux”中使用createProvider()方法。

I have installed the latest react-redux version(7.1.0), but am getting the error like "createProvider is not exported from react-redux" .我已经安装了最新的 react-redux 版本(7.1.0),但出现了类似“createProvider 未从 react-redux 导出”之类的错误。 When i gone through the node modules, i couldn't able to find the createProvider inside the src of react-redux.当我浏览节点模块时,我无法在 react-redux 的 src 中找到createProvider Is it a version issue or did i miss something in the code.是版本问题还是我错过了代码中的某些内容。 I have shared you the following code snippet as :我与您分享了以下代码片段:

Provider.js提供者.js

import { createProvider } from "react-redux";

export const STORE_KEY = "myComponentStore";    
export const Provider = createProvider(STORE_KEY);

TestComponent.js测试组件.js

import React, { Component } from "react";
import { createStore } from "redux";
import Mycomponent from "./MyComponent";

import { Provider } from "./Provider";

const reducer = {};

const initialState = {
  title: "multiple store"
};

const store = createStore(reducer, initialState);

class TestComponent extends Component {
  render() {
    return (
      <Provider store={store}>
        <Mycomponent />
      </Provider>
    );
  }
}
export default TestComponent;

Mycomponent.js我的组件.js

import React, { Component } from "react";

import { connect } from "./Connect";

class MyComponent extends Component {
  render() {
    return <div>{this.props.title}</div>;
  }
}

export default connect(function mapStateToProps(state) {
  return {
    title: state.title
  };
})(MyComponent);

似乎自 V6 以来已弃用删除

Your store could be something like this:您的商店可能是这样的:

// store.js

import { createStore } from 'redux';
import rootReducer from './root-reducer';

export default createStore(rootReducer);

And that "rootReducer" is a combination of different reducer files:而“rootReducer”是不同减速器文件的组合:

//root-reducer.js

import SomeReducers from './reducers/some-reducers';
import AnotherOne from './reducers/another-one';

const rootReducer = combineReducers({
  SomeReducers,
  AnotherOne,
})

export default rootReducer;

Then your store is used in the index然后你的商店在index

// index.js

import React from 'react';
import ReactDOM from 'react-dom';
import { Provider } from "react-redux";
import store from "./storage/store";
import App from './App';

ReactDOM.render(
  <Provider store={store}>
    <App />
  </Provider>
  , document.getElementById('root')
);

But it still just one store但它仍然只是一家store

In documentation , it says文档中,它说

Don't create more than one store in an application!不要在一个应用程序中创建多个商店! Instead, use combineReducers to create a single root reducer out of many.相反,使用 combineReducers 从许多中创建一个根减速器。

So one provider, one store and multiple reducers are the correct(recommended) way to use redux.所以一个提供者、一个存储和多个减速器是使用 redux 的正确(推荐)方式。 Create one global provider for your application and define your reducers for that provider.为您的应用程序创建一个全局提供程序并为该提供程序定义您的减速器。 You can use data selectors for your components.您可以为组件使用数据选择器。

For this information probably dev team decided to deprecate createProvider functionality.对于此信息,开发团队可能决定弃用createProvider功能。

I am trying to create multiple distinct Redux stores我正在尝试创建多个不同的 Redux 存储

It's fine to have multiple and distinct Redux stores in a single React application.在单个 React 应用程序中拥有多个不同的 Redux 存储是很好的。 Just don't use createProvider() for it.只是不要使用createProvider() The two approaches to having multiple independent Redux stores are:拥有多个独立 Redux 存储的两种方法是:

SubApp5.js
----------
const store = createStore(reducer)

return {
  <>
    <Provider store={store}>
      <MyTable />
    </Provider>
  </>
}

With this approach multiple Redux stores will coexist.使用这种方法,多个 Redux 存储将共存。

  • Use multiple SPAs inside a single React application - see crisp-react .在单个 React 应用程序中使用多个 SPA - 请参阅清晰反应 With this approach multiple Redux stores will not coexist.使用这种方法,多个 Redux 存储将不会共存。
    When you build crisp-react, it creates a React application with 2 SPAs and you can add Redux to each SPA.当您构建清晰反应时,它会创建一个带有 2 个 SPA 的 React 应用程序,您可以将 Redux 添加到每个 SPA。 Then you can switch from one SPA (and its Redux store) to another SPA and its store.然后您可以从一个 SPA(及其 Redux 存储)切换到另一个 SPA 及其存储。

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

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