1.state属性
简介
- state是组件对象最重要的属性, 值是对象(可以包含多个key-value的组合)
- 把组件看成是一个状态机(State Machines) 通过更新组件的state来更新对应的页面显示(重新渲染组件)
基础实例
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| class Weather extends React.Component { constructor(props) { super(props); this.state = { isHot: true }; this.changeWeather = this.changeWeather.bind(this); } render() { const {isHot} = this.state; return <h1 onClick={this.changeWeather}>今天天气很{isHot?'炎热':'凉爽'}</h1>; } changeWeather(){ const isHot = this.state.isHot; this.setState({isHot:!isHot}); } } ReactDOM.render(<Weather />,document.getElementById('test'));
|
state简写方式
1 2 3 4 5 6 7 8 9 10 11
| class DemoName extends React.Component { state = { key: value }; render() { const {key} = this.state; return <h1 onClick={this.handle}></h1>; } handle = () =>{ const {key} = this.state; this.setState({key:!key}); } }
|
Props属性
简介
- 每个组件对象都会有
props
(properties的简写)属性
- 组件标签的所有属性都保存在props中
- 内部读取某个属性值
this.props.propertyName
- 作用:通过标签属性从组件外 向组件内传递数据(只读 read only)
- 对props中的属性值进行类型限制和必要性限制
基础实例
类组件
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| class Person extends React.Component { render() { const { name,sex,age } = this.props; return ( <ul> <li>姓名:{name}</li> <li>性别:{sex}</li> <li>年龄:{age}</li> </ul> ); } } const p = { name:'Jarry',age:18,sex:'女'}; ReactDOM.render(<Person {...p} />, document.getElementById("test"));
|
函数组件
1 2 3 4 5 6 7 8 9 10 11 12
| function Person(props){ const {name,age,sex} = props; return( <ul> <li>姓名:{name}</li> <li>性别:{sex}</li> <li>年龄:{age}</li> </ul> ) } const p = { name:'Jarry',age:18,sex:'女'}; ReactDOM.render(<Person {...p} />, document.getElementById("test"));
|
对Props进行约束限制
类组件的约束形式
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
| class Person extends React.Component { static propTypes ={ name: PropTypes.string.isRequired, sex: PropTypes.string, age: PropTypes.number } static defaultProps ={ sex:'保密', age:18 } render() { const { name,sex,age } = this.props; return ( <ul> <li>姓名:{name}</li> <li>性别:{sex}</li> <li>年龄:{age}</li> </ul> ); } } const p = { name:'Jarry'}; ReactDOM.render(<Person {...p} />, document.getElementById("test"));
|
对函数组件的约束形式
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| function Person(props){ const {name,age,sex} = props; return( <ul> <li>姓名:{name}</li> <li>性别:{sex}</li> <li>年龄:{age}</li> </ul> ) }
Person.propTypes ={ name: PropTypes.string.isRequired, sex: PropTypes.string, age: PropTypes.number }
Person.defaultProps ={ sex:'保密', age:18 } const p = { name:'Jarry',age:18,sex:'女'}; ReactDOM.render(<Person {...p} />, document.getElementById("test"));
|
props与state的区别
Refs属性
简介
- 组件内的标签可以定义ref属性类标识自己,有点类似与JS中的id
- React文档中再三强调,请不要过度使用refs,所以当我们可以用dom原生对象解决时,尽量不要使用refs 依照之前的写法,首先是给出类组件和函数组件中refs的写法
三种编码形式
1.字符串形式的ref(官方不推荐)
1 2 3 4 5 6 7 8 9 10
| class Demo extends React.Component { showblurData = () =>{ alert(this.refs.input.value); } render() { return ( <input ref='input' type='text' placeholder='失去焦点展示数据' onBlur={this.showblurData} /> ); } }
|
2.函数回调形式的ref
1 2 3 4 5 6 7 8 9 10 11 12
| class Demo extends React.Component { showblurData = () =>{ alert(this.input2.value); } render() { return ( <input ref={ c=> this.input2 = c} type='text' placeholder='失去焦点展示数据' onBlur={this.showblurData} /> ); } }
|
3.createRef创建ref容器(官方推荐)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| class Demo extends React.Component {
myRef = React.createRef(); showData = () =>{ alert(this.myRef.current.value); } render() { return ( <div> <input ref={this.myRef} placeholder="点击按钮提示数据" type="text" /> <button onClick={this.showData}>点击我提示左边的数据</button> </div> ); } }
|