所以我有相当标准的代码仍然无法正常工作。这里有什么问题?
我尝试了这个解决方案,因为我有同样的错误:solution,但没有成功。 我在浏览器控制台中的错误仍然是:
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.
我的代码是:
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__________ 还请阅读评论,确实有用的信息
答案 0 :(得分:3)
Provider
是react-redux
模块的一部分;不是全部。
您需要从
更改您的require语句var Provider = require('react-redux');
到
var Provider = require('react-redux').Provider;
或使用ES6模块语法
import { Provider } from 'react-redux';
答案 1 :(得分:0)
当你使用带有大括号的箭头函数时,它与你的函数返回对象不同。括号表示函数体(因此必须在其中使用return语句)。以下是它的假设:
connect(mapStateToProps, {
test: () => {return { type: 'TEST_ACTION' }}
})(Index);