React 组件实例的三大属性
React笔记
props基本使用
批量传递props / 批量传递标签属性
<body> <div id="test1"></div> <div id="test2"></div> <div id="test3"></div> <script type="text/babel"> class Person extends React.Component{ render(){ const {name,age,sex} = this.props return ( <ul> <li>姓名: {name}</li> <li>性别: {sex}</li> <li>年龄: {age+1}</li> </ul> ) } } ReactDOM.render(<Person name="jerry" age={19} sex="男"/>,document.getElementById('test1')) ReactDOM.render(<Person name="tom" age={18} sex="女"/>,document.getElementById('test2'))
const p = {name:'兰莉',age:18,sex:'女'}
{...p} 中{ }是转义字符, 实际上是..p发挥作用, ..p仅在标签对中可以使用 ReactDOM.render(<Person {...p}/>,document.getElementById('test3')) </script> </body>
|
展开运算符
数组加减
<body> <script type="text/javascript" > let arr1 = [1,3,5,7,9] let arr2 = [2,4,6,8,10] console.log(...arr1); let arr3 = [...arr1,...arr2] function sum(...numbers){ return numbers.reduce((preValue,currentValue)=>{ return preValue + currentValue }) } console.log(sum(1,2,3,4)); </script> </body>
|
克隆
<body> <script type="text/javascript" > let person = {name:'tom',age:18} let person2 = {...person} person.name = 'jerry' console.log(person2); console.log(person); </script> </body>
|
合并
<body> <script type="text/javascript" > let person3 = {...person,name:'jack',address:"地球"} console.log(person3); </script> </body>
|
限制
<body> <!-- 引入prop-types, 用于对组件标签属性进行限制 --> <script type="text/javascript" src="../js/prop-types.js"></script> <script type="text/babel"> class Person extends React.Component{ render(){ const {name,age,sex} = this.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, speak:PropTypes.func, } Person.defaultProps = { sex:'男女同体', age:"已经死了" } ReactDOM.render(<Person name="哈哈哈" speak={speak}/>,document.getElementById('test1')) ReactDOM.render(<Person name="tom" age={18} sex="女"/>,document.getElementById('test2'))
const p = {name:'老刘',age:18,sex:'女'} ReactDOM.render(<Person {...p}/>,document.getElementById('test3'))
function speak(){ console.log('我会讲话了'); } </script> </body>
|
构造器能省略尽量省略
costructor(props){ console.log(props) super(props) console.log('costructor',props); }
state = {}
demo = ()=>{ }
|
函数式组件使用props
<body> <script type="text/babel"> functiom Person (props){ const { name, age, sex } = props return ( <ul> <li>姓名:{name}</li> <li>性别:{sex}</li> <li>年龄:{age}</li> </ul> ) } ReactDOM.render(<Person name="史迪奇" age="未知" sex="未知"/>,document.getElementById('test1')) </script> </body>
|
函数式组件也可以进行限制
<body> <script type="text/babel"> functiom 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.string, }
Person.defaultProps = { sex:'未知', age:"未知" } ReactDOM.render(<Person name="史迪奇" age="未知" sex="未知"/>,document.getElementById('test1')) </script> </body>
|
refs
字符串写法 不建议用
<body> <script type="text/babel"> class Demo extends React.Component{ showData = ()=>{ const {input1} = this.refs alert(input1.value) } showData2 = ()=>{ const {input2} = this.refs alert(input2.value) } render(){ return( <div> <input ref="input1" type="text" placeholder="点击按钮提示数据"/> <button onClick={this.showData}>点我提示左侧的数据</button> <input ref="input2" onBlur={this.showData2} type="text" placeholder="失去焦点提示数据"/> </div> ) } } ReactDOM.render(<Demo a="1" b="2"/>,document.getElementById('test')) </script> </body>
|
字符串写法效果
回调函数的ref
解释: 定义但没调用
特点
- 定义
- 调2用
- 执行
<body> <script type="text/babel"> class Demo extends React.Component{ showData = ()=>{ const {input1} = this alert(input1.value) } showData2 = ()=>{ const {input2} = this alert(input2.value) } render(){ return( <div> {/* cref={c => this.input1 = c }是回调函数 剩下的交给React 仅ref */} <input ref={c => this.input1 = c } type="text" placeholder="点击按钮提示数据"/> <button onClick={this.showData}>点我提示左侧的数据</button> <input onBlur={this.showData2} ref={c => this.input2 = c } type="text" placeholder="失去焦点提示数据"/> </div> ) } } ReactDOM.render(<Demo a="1" b="2"/>,document.getElementById('test')) </script> </body>
|
回调函数效果
回调ref函数执行次数
<body> <script type="text/babel"> class Demo extends React.Component{
state = {isPai:false}
showInfo = ()=>{ const {input1} = this alert(input1.value) }
changeWeather = ()=>{ const {isPai} = this.state this.setState({isPai:!isPai}) }
saveInput = (c)=>{ this.input1 = c; console.log('@',c); }
render(){ const {isPai} = this.state return( <div> <h2>派大星是{isPai ? '大聪明':'骚话王'}</h2> {/*<input ref={(c)=>{this.input1 = c;console.log('@',c);}} type="text"/><br/><br/>*/} <input ref={this.saveInput} type="text"/><br/><br/> <button onClick={this.showInfo}>点我提示输入的数据</button> <button onClick={this.changeWeather}>点击切换头衔</button> </div> ) } } ReactDOM.render(<Demo/>,document.getElementById('test')) </script> </body>
|
回调ref函数执行次数
createRef的使用
createRef的官方推荐, alert(this.sDq.current.value) 用几个就要保证上方的 sDq = React.createRef() 就是需要创建的几个Ref的容器
<body> <script type="text/babel"> class Demo extends React.Component{
sDq = React.createRef() sDz = React.createRef() showData = ()=>{ alert(this.sDq.current.value); } showData2 = ()=>{ alert(this.sDz.current.value); } render(){ return( <div> <input ref={this.sDq} type="text" placeholder="点击按钮提示数据"/> {/* 点击焦点 onClick */} <button onClick={this.showData}>点我提示左侧的数据</button> {/* 失去焦点onBlur */} <input onBlur={this.showData2} ref={this.sDz} type="text" placeholder="失去焦点提示数据"/> </div> ) } } ReactDOM.render(<Demo a="1" b="2"/>,document.getElementById('test')) </script> </body>
|
回调ref函数执行次数
作者: 我叫史迪奇
本文来自于:
https://sdq3.link/React-state-props-refs.html博客内容遵循 署名-非商业性使用-相同方式共享 4.0 国际 (CC BY-NC-SA 4.0) 协议