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文档部分以获取一些解释.
归档时间: |
|
查看次数: |
6131 次 |
最近记录: |