一、input类似于vue中v-model
//value是可变的,input要加上onChange事件<input type="text" value={this.state.userName} onChange={this.handleChange}/>this.state={userName}handleChange=(e)=>{this.setState({userName:e.target.value})}
例如
<td><InputNumber min={1} defaultValue={item.productCount}onChange={this.ChangeNum.bind(this,item)} /></td>
ChangeNum=(item,value)=>{var id=item.id;var data=this.state.data;data.forEach(item=>{if(item.id==id){item.productCount=value}})this.setState({data})}
二、三元表达式
this.state={isShow:true}render() {return(<div><p>{this.state.isShow?'good':'hello world'}</p></div>)}
三、Class,Style
className:className={`list ${!item.isSelected?'':'hidden'}`}
style={{display: (item.isSelected) ? "" : "none"}}
四、if-else
render() {return(<div>{this.Message()}</div>)}Message(){if(this.state.isShow){return (<span>显示</span>)}else{return (<span>隐藏</span>)}}
五、获取Dom
<button ref={(btn)=>{this.btn=btn}}>ref</button>componentDidMount(){console.log(this.btn) //<button>ref</button>}
六、导入本地图片
<img src={'../assets/banner_1.jpg'} alt=""/>
<img src={require('./assets/01.jpg')}></img>
七、遍历对象
遍历cities对象
http://yapi.demo.qunar.com/mock/43104/film
组件中
class City extends Component {constructor(props){super(props);this.state={cities:"",}}<div className="cities">{Object.keys(this.state.cities).map((obj,value)=>{return (<div key={value}>{obj}{this.state.cities[obj].map((item,index)=>{return (<p onClick={this.handleCity.bind(this,item.name)}key={index}>{item.name}</p>)})}</div>)})}</div>
八、严格模式
React.StrictMode 是一个有用的组件,用于突出显示应用程序中的潜在问题。就像 <Fragment>,<StrictMode> 一样,它们不会渲染任何额外的 DOM 元素。它为其后代激活额外的检查和警告。这些检查仅适用于开发模式。
import React from 'react'function ExampleApplication() {return (<div><Header /><React.StrictMode><div><ComponentOne /><ComponentTwo /></div></React.StrictMode><Footer /></div>)}
在上面的示例中,strict mode 检查仅应用于 <ComponentOne> 和 <ComponentTwo> 组件。
