注释大家就不要看了,都是我乱注释的感觉应该有错的···
index.js
import 'core-js/fn/object/assign';
import React from 'react';
import ReactDOM from 'react-dom';
import App from './components/Main';
import {createStore} from 'redux';
import {Provider} from 'react-redux';
import todoApp from 'stores/reducers.js';
let store = createStore(todoApp);
// Render the main component into the dom
ReactDOM.render(
<Provider store = {store}>
<App />
</Provider>,
document.getElementById('app')
);
main.js
require('normalize.css/normalize.css');
require('styles/App.css');
import React from 'react';
import {connect} from 'react-redux';
import {changeStyle} from '../actions/actions.js'
import yeomanImage from '../images/yeoman.png'
//let yeomanImage = require('../images/yeoman.png');
class AppComponent extends React.Component {
constructor(props) {
super(props);
}
handleClick(e){
this.props.changeStyles;
}
render() {
//从组件的props属性中导入actions里的方法和一个变量
const {dispatch,propsStyle} = this.props;
return (
<div className="index">
<img src={yeomanImage} alt="Yeoman Generator" />
{propsStyle}
{/*<div style={stateStyle} onClick={this.handleClick.bind(this)}>Please edi to get started!</div>*/}
<div changeStyle={dispatch(changeStyle())} onClick={this.handleClick.bind(this)}>Please edi to get started!</div>
</div>
);
}
}
function select(state) {
return {
//将我们需要的state中的数据绑定到props上
propsStyle:state.propsStyle
}
}
export default connect(select)(AppComponent);
//export default AppComponent;
action.js
/**
* Created by zhangpeng on 2016/9/18.
*/
export const change = 'change';
export function changeStyle() {
return {type:change}
}
reducer.js
import {combineReducers} from 'redux';
import {change} from '../actions/actions.js'
function propsStyle(state=0,action) {
switch (action.type){
case change:
return state +1
default: //必须有
return state
}
}
const todoApp = combineReducers({
propsStyle //这个属性名propsStyle必须和函数propsStyle同名 表示通过这个函数处理后的state的值为state.propsStyle connect会将state.propsStyle赋值到props
})
export default todoApp;
我也不知道是哪里理解错了,直接显示了最终结果,而且报了很多下边的错误
大神救救我,我看了很久了都没搞懂这个····
不要在 render 里去改变状态,说的就是这里
changeStyle={dispatch(changeStyle())}
。