首页 > 解决方案 > 在 props 中传递 Redux store

问题描述

我正在大学练习使用 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

我的文件是这些:

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();

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);
    }
}

应用程序.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);

减速器.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;

到这个时候,我至少应该能够在没有任何错误的情况下运行应用程序,但我总是在 Redux Store 中遇到错误,我不知道为什么。可能是任何模块的版本或类似的问题吗?

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

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

标签: node.jsreactjsreduxreact-reduxyarnpkg

解决方案


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

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

请参阅我的帖子Idiomatic Redux:React-Redux 的历史和实现,了解有关 API 的那部分被删除的一些详细信息。

另外,请注意,从 a 返回整个 Redux 状态对象mapState通常不是一个好主意,如果由于某种原因您确实想要这样做,则不需要传播它 - 只需return state. 有关一些解释,请参阅有关编写mapState函数的 React-Redux 文档部分。


推荐阅读