在道具中传递Redux商店

Lui*_*cri 5 node.js reactjs redux react-redux yarnpkg

我正在大学练习使用React和Redux构建应用程序.当我使用Yarn启动服务器时,我收到此错误:

Passing redux store in props has been removed and does not do anything. 
To use a custom Redux store for specific components, create a custom React context with React.createContext(), and pass the context object to React-Redux's Provider and specific components like: 
<Provider context={MyContext}><ConnectedComponent context={MyContext} /></Provider>. 
You may also pass a {context : MyContext} option to connect
Run Code Online (Sandbox Code Playgroud)

我的文件是这些:

index.js

import React from 'react';
import ReactDOM from 'react-dom';
import './index.css';
import ReduxProvider from './redux/ReduxProvider';
import * as serviceWorker from './serviceWorker';

ReactDOM.render(<ReduxProvider />, document.getElementById('root'));

serviceWorker.unregister();
Run Code Online (Sandbox Code Playgroud)

ReduxProvider

import { questions } from "../assets/mock-data.js";
import { Provider } from 'react-redux';
import GlobalState from "./reducers";
import { createStore } from 'redux';

import React from 'react';
import App from '../App';

export default class ReduxProvider extends React.Component {
    constructor(props) {
        super(props);
        this.initialState = {
            score: 0,
            finished: false,
            currentQuestion: 0,
            questions: [ ...questions]
        };
        this.store = this.configureStore();
    }

    render() {
        return (
            <Provider store={ this.store }>
                <div>
                    <App store={ this.store } />
                </div>
            </Provider>
        );
    }

    configureStore() {
        return createStore(GlobalState, this.initialState);
    }
}
Run Code Online (Sandbox Code Playgroud)

App.js

import React, { Component } from 'react';
import './App.css';
import { connect } from 'react-redux';

class App extends Component {
  render() {
    return (
      <div className="App">
        Hola
      </div>
    );
  }
}

function mapStateToProps(state) {
    return {
      ...state
    };
}

export default connect(mapStateToProps)(App);
Run Code Online (Sandbox Code Playgroud)

reducers.js

import { combineReducers } from 'redux';

function score(state = 0, action = {}) {
    switch(action.type) {
        default:
            return state;
    }
}

function finished(state = 0, action = {}) {
    switch(action.type) {
        default:
            return state;
    }
}

function currentQuestion(state = 0, action = {}) {
    switch(action.type) {
        default:
            return state;
    }
}

function questions(state = 0, action = {}) {
    switch(action.type) {
        default:
            return state;
    }
}

const GlobalState = (combineReducers({
    score,
    finished,
    currentQuestion,
    questions
}));

export default GlobalState;
Run Code Online (Sandbox Code Playgroud)

到这个时候,我应该能够至少运行应用程序没有任何错误,但我总是得到Redux商店的错误,我不知道为什么.这可能是任何模块的版本或类似的问题?

我正在使用纱线1.12.3和nodejs v8.12.0

告诉我是否还有其他需要显示的文件.

mar*_*son 12

不要将store实例传递给<App>.这没有任何作用,React-Redux v6正在警告你.

在React-Redux v5中,将商店作为道具直接传递给允许连接的组件,并且在极少数情况下它很有用,但在v6中已被删除.

请参阅我的帖子Idiomatic Redux:React-Redux的历史和实现,了解有关为什么API部分被删除的一些细节.

另请注意,从返回整个Redux的状态对象mapState通常不是一个好主意,如果由于某种原因,你真正想做的事情是,你不需要传播它-只是return state.有关编写mapState函数的信息,请参阅React-Redux文档部分以获取一些解释.


and*_*dyb 5

React Redux 7.0.1允许store再次作为道具传递。

从发行说明中:

我们带回了将商店作为道具直接传递给连接的组件的功能。由于内部实现的更改(组件不再直接订阅商店),在版本6中将其删除。一些用户表示担心在单元测试中使用上下文是不够的。由于我们的组件再次使用直接订阅,因此我们重新实现了此选项,这样就可以解决这些问题。