We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
参考demo
// 无状态组件 const TodoList = ({ todos, toggleTodo }) => { return ( <div>aaa</div> ) }
// 有状态组件 有state,需要访问生命周期方法 class InputControlES6 extends React.Component { constructor(props) { super(props); // 设置 initial state this.state = { text: props.initialValue || 'placeholder' }; // 手动绑定this this.handleChange = this.handleChange.bind(this); } handleChange(event) { this.setState({ text: event.target.value }); } render() { return ( <input onChange={this.handleChange} value={this.state.text} /> ); } }
<input ref={node => input = node}>
React.Component
为了解决组件间的状态传递 启用redux 参考demo
为了解决store state dispatch一直往下传递 启用react-redux 参考demo
// 暴露state 此组件有了 this.props.todos 由state计算而来 const mapStateToProps = (state) => { return { todos: getFilterTodos(state.todos, state.visibleFilter) } } // 暴露dispatch 此组件有了this.props.toggleTodoClick 执行这个函数有相应的dispatch const mapDispatchToProps = (dispatch) => { return { toggleTodoClick: (id) => { dispatch(toggleTodo(id)) } } }
// state.values.value1 state.values.value2变化才会重新执行totalSelector const totalSelector = createSelector( [ state => state.values.value1, state => state.values.value2 ], (value1, value2) => value1 + value2 )
参考demo actionCreator return object,无法在里面做异步操作,所以引入redux-thunk。actionCreator可以 return function,并且暴露dispatch用于发action,暴露getState用于拿到所有state。 vuex中不存在以上问题(action直接写函数然后在里面emmit mutations,rootState可以拿到所有state) 所以不需要thunk这种东西
// 普通的actionCreator const fetchPosts = postId => { return { type: 'FETCHPOSTS' postId } } // redux-thunk actionCreator return function const fetchPosts = postId => { return (dispatch, getState) => { return fetch(`/some/API/${postId}.json`) .then(json => dispatch(receivePosts(postId, json))); }; }
参考链接
@inject('todosStore') @observer
@inject('todosStore') @observer class AddTodo extends Component { constructor (props, context) { } render () { const { todosStore } = this.props return ( <div> // this.props.todosStore调用action changeAddInputValue addInputValue, // 调用state addInputValue <input type="text" onChange={(e) => todosStore.changeAddInputValue(e.target.value)} value={todosStore.addInputValue} /> <button onClick={() => todosStore.addTodo(todosStore.addInputValue)}>Add todo</button> </div> ) } }
The text was updated successfully, but these errors were encountered:
No branches or pull requests
提纲
react
参考demo
react主要构成
无状态组件 有状态组件
<input ref={node => input = node}>
访问domReact.Component
这种es6形式创建组件从上到下的数据流
redux
为了解决组件间的状态传递 启用redux
参考demo
apis
react-redux
为了解决store state dispatch一直往下传递 启用react-redux
参考demo
apis
tips
数据扁平化主要是为了
redux-thunk
参考demo
actionCreator return object,无法在里面做异步操作,所以引入redux-thunk。actionCreator可以 return function,并且暴露dispatch用于发action,暴露getState用于拿到所有state。
vuex中不存在以上问题(action直接写函数然后在里面emmit mutations,rootState可以拿到所有state) 所以不需要thunk这种东西
redux-saga
参考demo
redux文件组织
参考链接
mobx
参考demo
@inject('todosStore') @observer
到react类里面,this.props.todosStore调用action statevuex与redux全家桶比较
大致类比
所以不需要redux-thunk
为了在组件里dispatch和拿state需要react-redux
对比
react建议学习路线
The text was updated successfully, but these errors were encountered: