简体   繁体   English

Redux + React-Router不呈现

[英]Redux + React-Router doesn't render

So I have pretty standard code which is still doesn't work. 所以我有相当标准的代码仍然无法正常工作。 What's wrong here? 这有什么不对?

I tried this solution as I have the same errors: solution , but of no success. 我试过这个解决方案,因为我有同样的错误: 解决方案 ,但没有成功。 my errors in browser console still are: 我在浏览器控制台中的错误仍然是:

Warning: React.createElement: type should not be null, undefined, boolean, or number. It should be a string (for DOM elements) or a ReactClass (for composite components)

Uncaught Error: Invariant Violation: Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: object.

My code is: 我的代码是:

var React = require('react');
var ReactDOM = require('react-dom');
var Router = require('react-router').Router;
var Route = require('react-router').Route;
var createBrowserHistory = require('history/lib/createBrowserHistory');
var createStore = require('redux').createStore;
var Provider = require('react-redux');
var connect = require('react-redux').connect;

var options = 'someTxt';

var reducer = function (state = {}, action) {
    console.log('reducer was called with state', state, 'and action', action)
    return state;
};

const store = createStore(reducer, options);

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

var Index = React.createClass({
    render: function () {
        return (
            <div>
                {this.props.options}
            </div>
        );
    }
});

connect(mapStateToProps, {
    test: () => { type: 'TEST_ACTION' }
})(Index);

ReactDOM.render(
    <Provider store={store}>
        {() =>
            <Router history={createBrowserHistory()}>
                <Route path="/" component={Index}/>
            </Router>}
    </Provider>,
    document.getElementById("container")
);

__________UPDATE__________ Please also read comments, there are really useful information __________UPDATE__________也请阅读评论,确实有用的信息

Provider is part of the react-redux module; Providerreact-redux模块的一部分; not all of it. 不是全部。

You need to change your require statement from 您需要更改您的require语句

var Provider = require('react-redux');

to

var Provider = require('react-redux').Provider;

or with ES6 module syntax 或者使用ES6模块语法

import { Provider } from 'react-redux';

When you use arrow functions with bracets it's not the same that your function returns object. 当你使用带有大括号的箭头函数时,你的函数返回对象是不一样的。 The bracets means function body (so you must use return statement in it). 括号表示函数体(因此必须在其中使用return语句)。 Here is how it suppose to be: 以下是它的假设:

connect(mapStateToProps, {
    test: () => {return { type: 'TEST_ACTION' }}
})(Index);

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

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