因此,我尝试使用此示例React-router
进行配置,以设置基本应用程序。Redux
当我尝试渲染我的组件时,我收到 1 个错误和 2 个警告。
错误是:Uncaught TypeError: Cannot read property 'toUpperCase' of undefined
。
警告 #1:Warning: React.createElement: type should not be null or undefined. It should be a string (for DOM elements) or a ReactClass (for composite components).
警告#2:Warning: Only functions or strings can be mounted as React components.
我刚得到两个简单的组件。一个Root
组件和一个Container
组件来测试一切是否正常。
根组件的内容:
import React, { Component } from 'react';
import { Provider } from 'react-redux';
import { createHistory } from 'history';
import { createStore, combineReducers } from 'redux';
import { Router, Route } from 'react-router';
import timeline from '../reducers/timeline';
import Container from '../components/Container';
const store = createStore(combineReducers({timeline}));
store.subscribe(() => {
console.info(store.getState());
})
const history = createHistory();
React.render(
<Provider store={store}>
{() =>
<Router history={history}>
<Route path="/" component={Container}/>
</Router>
}
</Provider>,
document.getElementById('root')
);
Container 组件的内容:
import React, { Component } from 'react';
import { connect } from 'react-redux';
class Container extends Component {
render() {
return (
<div id="outter">
Container
</div>
);
}
}
function mapStateToProps(state) {
console.log(state);
return {
test: state,
};
}
export default connect(
mapStateToProps,
{
type: 'TEST_ACTION',
}
)(Container);
以防万一 - 减速器的内容:
const initialState = [
{id: 1, message: 'Test message'},
];
export default function timeline(state = initialState, action = {}) {
switch (action.type) {
case 'TEST_ACTION':
return initialState;
default:
return state;
}
}