React问题总结与归纳
2016-04-13 00:17
731 查看
欢迎大家指导与讨论 : )
【持续更新】本文主要记录笔者在学习中遇到的问题,并作出相应总结。有错误的地方希望各位能够指出。
一、在es6中getInitialState( 摘要: constructor(props)和this.state )
二、在es6中setState( 摘要: fun.bind(this)和this.setState )
三、PureRenderMixn在深比较会失效(摘要: Immutable提升性能)
export default class TabHeader extends Component {
shouldComponentUpdate(nextProps){
return !Immutable.is(Immutable.fromJS(nextProps), Immutable.fromJS(this.props));
}
//..........
}
四、Map结构的state由reducer传到组件时需要先提取[b]this.props中的某个属性(摘要: this.props为多对象集合体)[/b]
五、reducer state中覆盖原object类型的state简洁写法(摘要: Object.assign())
六、获取动态render组件的真实DOM节点及其value值(摘要: fun.bind(this) / ReactDOM.findDom())
七、父组件向子组件传递方法(摘要: React.cloneElement)
八、不同浏览器对对象更新/对象深比较的兼容性问题(underscore的extend、omit)
十一、在.js文件中使用async( 摘要: 配置webpack )
十、ant design的webpack配置
【持续更新】本文主要记录笔者在学习中遇到的问题,并作出相应总结。有错误的地方希望各位能够指出。
一、在es6中getInitialState( 摘要: constructor(props)和this.state )
/*es6*/ class TodoList extends Component{ constructor(props){ super(props); this.state = { items: ['hello', 'world', 'click', 'me'] } } render(){ //.. } } /*es5*/ var TodoList = React.createClass({ getInitialState: function() { return {items: ['hello', 'world', 'click', 'me']}; }, render(){ //... } })
二、在es6中setState( 摘要: fun.bind(this)和this.setState )
/*es6*/ class TodoList extends Component{ render(){ return ( <div> <button onClick={ this.handleAdd.bind(this) } >Add item</button> </div> ) } handleAdd(){ var newItem = prompt('enter a new') var newItems = [ ...this.state.items, newItem ]; this.setState({items: newItems}) } }
三、PureRenderMixn在深比较会失效(摘要: Immutable提升性能)
/ assume this.props.value is { foo: 'bar' } // assume nextProps.value is { foo: 'bar' }, // but this reference is different to this.props.value this.props.value !== nextProps.value; //true //错误:浅比较/引用对象的比较/this.props和nextProp实际上是两个不同的对象引用 /*正确做法1-------对象比较应该进行深比较*/ shouldComponentUpdate: function(nextProps, nextState) { return this.props.value.foo !== nextProps.value.foo; } /*正确做法2-------使用Immutable提升对象深比较的性能*/
export default class TabHeader extends Component {
shouldComponentUpdate(nextProps){
return !Immutable.is(Immutable.fromJS(nextProps), Immutable.fromJS(this.props));
}
//..........
}
四、Map结构的state由reducer传到组件时需要先提取[b]this.props中的某个属性(摘要: this.props为多对象集合体)[/b]
export default class TabHeader extends Component { shouldComponentUpdate(nextProps){ return !Immutable.is(Immutable.fromJS(nextProps), Immutable.fromJS(this.props)); } render (){ const active = this.props.tabIndex return ( //.... ) } //.... }
五、reducer state中覆盖原object类型的state简洁写法(摘要: Object.assign())
Object.assign(...state, { third: { value: '', show: true } }) //原state结构为 const initState = { first: { value: '', show: true }, second: { value: '', show: false }, third: { value: '', show: false } }
六、获取动态render组件的真实DOM节点及其value值(摘要: fun.bind(this) / ReactDOM.findDom())
export default class CascadeBar extends Component{ render(){ return ( <div> {this.renderFirst()} </div> ) } renderFirst(){ //if...render... return( <select onChange={this.countryChange.bind(this)} ref="countryInput"> //..... </select> ) } countryChange(){ ReactDOM.findDOMNode(this.refs.countryInput).value } }
七、父组件向子组件传递方法(摘要: React.cloneElement)
<div className="Detail"> {this.props.children && React.cloneElement(this.props.children, { helloWorld: this.helloWorld })} </div>
八、不同浏览器对对象更新/对象深比较的兼容性问题(underscore的extend、omit)
// 无效手机UC不支持 return Object.assign(state, {left: {show: action.value}} // 有效->克隆了一个新对象 var e = _.omit(state) return _.extend(state , {left: {show: action.value}})
十一、在.js文件中使用async( 摘要: 配置webpack )
loaders: [ { test: /\.js|jsx$/, loader: 'babel', query: { presets: ['es2015', 'stage-3'], plugins: ['transform-runtime'] }, exclude: /node_modules/ } ]
十、ant design的webpack配置
//package.json { "name": "processBar", "version": "1.0.0", "description": "", "main": "index.js", "scripts": { "test": "echo \"Error: no test specified\" && exit 1" }, "author": "", "license": "ISC", "dependencies": { "antd": "^0.12.14", "atool-build": "^0.6.6", "babel-loader": "^6.2.4", "babel-plugin-antd": "^0.3.2", "babel-preset-es2015": "^6.6.0", "babel-preset-react": "^6.5.0", "css-loader": "^0.23.1", "es3ify-loader": "^0.2.0", "react": "^0.14.7", "react-dom": "^0.14.7", "style-loader": "^0.13.1", "webpack": "^1.12.14" } } //webpack.config.js var path = require('path') var webpack = require('webpack') module.exports = { entry: [ path.resolve(__dirname, 'index.js') ], output: { path: 'dist/', filename: '[name].js' }, resolve: { extensions: ['', '.js', '.jsx'] }, module: { loaders: [ { test: /\.js|jsx/, loader: 'es3ify-loader' }, { test: /\.js|jsx$/, loader: 'babel', query: { presets: ['es2015', 'react'], cacheDirectory: true, plugins: [ 'babel-plugin-antd' ] } }, { test: /\.css$/, loader: 'style-loader!css-loader' } ] } }
相关文章推荐
- 关于react native android navigationBar的隐藏问题
- react简介
- React-router匹配的组件 如何使用?
- React 处理form表单input输入项双向数据绑定
- React 更新数据array子项添加新的属性便于循环处理
- React 动态更新子项item从array数据
- React 级联下拉列表实现
- ReactNative初学笔记1.1flexbox布局
- React-Router JS控制路由跳转
- React Native之创建iOS视图
- React-Native系列Android——Touch事件原理及状态效果
- 高性能I/O设计模式Reactor和Proactor
- 【React Native 学习笔记】React Native For Android程序白屏
- React技术栈及前端技术总结
- 整理了一份React-Native学习指南
- 函数响应式编程(FRP)框架--ReactiveCocoa
- Sublime Text 3 搭建 React.js 开发环境
- Sublime Text 3 搭建 React.js 开发环境
- java学习-【转】什么是Reactor模式,或者叫反应器模式
- Eziriz.Net.Reactor使用注意事项