1.state
1.1.初始化状态
constructor (props) {
super(props)
this.state = {
stateProp1 : value1,
stateProp2 : value2
}
}
1.2.读取某个状态值
this.state.statePropertyName
1.3. 更新状态——>组件界面更新
this.setState({
stateProp1 : value1,
stateProp2 : value2
})
1.4.案例
<div id="exmaple"></div>
<script src="../js/react.development.js"></script>
<script src="../js/react-dom.development.js"></script>
<script src="../js/babel.min.js"></script>
<script type="text/babel">
//定义组件类
class Like extends React.Component{
//给状态赋初值
constructor(props){
super(props)
//初始化状态
this.state={
isLikeMe:false
}
//将新增方法的this强制绑定为组件对象,
//bind绑定this会产生新的函数,这里的this就是组件实例对象
this.handleClick=this.handleClick.bind(this)
}
//新添加方法:内部的this默认不是组件对象,而是underfined
handleClick(){
//得到状态取反
const isLikeMe=!this.state.isLikeMe
//更新状态
this.setState({isLikeMe})
}
//重写组件内方法,组件内本来就有
render(){
//读取状态
const {isLikeMe}=this.state
//这里是组件对象调用这个方法
return <h2 onClick={this.handleClick}>{isLikeMe?"你喜欢我":"我喜欢你"}</h2>
}
}
//渲染标签组件
ReactDOM.render(<Like/>,document.getElementById("exmaple"))
思路:首先定义组件对象,在组件对象上定义一个属性,属性值是布尔类型,用来切换文字显示的状态,在渲染阶段给标签属性绑定一个handClick事件,这里的handleClick要大写c,因为要区分跟普通的绑定事件,在事件内对定义的属性进行修改,因为这个事件是我们自己定义的,因此,这里的this是underfined,需要进行处理,使用bind对它进行操作,改变它的this指向,在把改变后的新函数,赋值给它原先的函数。
2.props
2.1.基本使用
通过在组件上定义属性,向组件内传递数据
ReactDOM.render(<Person name='王苏' age={18} sex="男" />, document.getElementById("app"))
批量传递
const p = { name: "wx", age: "13", sex: "女" }
ReactDOM.render(<Person {...p} />, document.getElementById("app1"))
2.2.案例
<div id="app"></div>
<div id="app1"></div>
<script src="../js/react.development.js"></script>
<script src="../js/react-dom.development.js"></script>
<script src="../js/babel.min.js"></script>
<script src="../js/prop-types.js"></script>
<script type="text/babel">
class Person extends React.Component {
// constructor(name, age) {
// super(props)
// }
render() {
let { name, age, sex } = this.props
return (
<ui>
<li>我的名字{name}</li>
<li>我的年龄{age + 1}</li>
<li>我的性别{sex}</li>
</ui>
)
}
}
//对标签属性的类型必要性进行限制
Person.propTypes = {
name: PropTypes.string.isRequired,
age: PropTypes.number,
sex: PropTypes.string
}
//指定默认的标签属性值
Person.defaultProps = {
sex: "男",
age: 19
}
//单独页面渲染
ReactDOM.render(<Person name='王苏' age={18} sex="男" />, document.getElementById("app"))
//整体渲染
const p = { name: "wx", age: "13", sex: "女" }
ReactDOM.render(<Person {...p} />, document.getElementById("app1"))
2.3.简化props
2.4.类组件中的props
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<div id="app"></div>
<div id="app1"></div>
<script src="../js/react.development.js"></script>
<script src="../js/react-dom.development.js"></script>
<script src="../js/babel.min.js"></script>
<script src="../js/prop-types.js"></script>
<script type="text/babel">
//构造器是否接受。取决与是否希望通过this拿到props
//类中的构造器能省略就省略
class Person extends React.Component {
constructor(props){
super(props)
console.log(this.props)
}
//static表示定义一个静态成员,该属性直接定义在类上
//对标签属性的类型必要性进行限制
static propTypes = {
name: PropTypes.string.isRequired,
age: PropTypes.number,
sex: PropTypes.string
}
//指定默认的标签属性值
static defaultProps = {
sex: "男",
age: 19
}
render() {
let { name, age, sex } = this.props
return (
<ul>
<li>我的名字{name}</li>
<li>我的年龄{age+1}</li>
<li>我的性别{sex}</li>
</ul>
)
}
}
//单独页面渲染
ReactDOM.render(<Person name='王苏'/>, document.getElementById("app"))
</script>
</body>
</html>
2.5.函数组件使用props
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<div id="app"></div>
<div id="app1"></div>
<script src="../js/react.development.js"></script>
<script src="../js/react-dom.development.js"></script>
<script src="../js/babel.min.js"></script>
<script src="../js/prop-types.js"></script>
<script type="text/babel">
function Person(props) {
const {name,age,sex}=props
return (
<ul>
<li>我的名字{name}</li>
<li>我的年龄{age + 1}</li>
<li>我的性别{sex}</li>
</ul>
)
}
//单独页面渲染
ReactDOM.render(<Person name='王苏' age={18} sex="男" />, document.getElementById("app"))
</script>
</body>
</html>
3.ref
3.1字符串类型的ref
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<div id="app"></div>
<script src="../js/react.development.js"></script>
<script src="../js/react-dom.development.js"></script>
<script src="../js/babel.min.js"></script>
<script src="../js/prop-types.js"></script>
<script type="text/babel">
class Input extends React.Component {
inputHandle = () => {
//通过this可以拿到refs,通过refs可以收集到虚拟dom的节点
let {value}=this.refs.input1
alert(value)
}
burHandle = () => {
let {value}=this.refs.input2
alert(value)
}
render() {
return <div>
<input type="text" placeholder="请输入文字" ref="input1" />
<button onClick={this.inputHandle}>确定</button>
<input type="text" placeholder="失去焦点弹出文字" onBlur={this.burHandle} ref="input2" />
</div>
}
}
ReactDOM.render(<Input />, document.getElementById("app"))
</script>
</body>
</html>
3.2回调函数的ref
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<div id="app"></div>
<script src="../js/react.development.js"></script>
<script src="../js/react-dom.development.js"></script>
<script src="../js/babel.min.js"></script>
<script src="../js/prop-types.js"></script>
<script type="text/babel">
class Input extends React.Component {
inputHandle = () => {
//通过this可以拿到refs,通过refs可以收集到虚拟dom的节点
let { input } = this
// alert(value)
console.log(input.value)
}
inputHandle1 = () => {
}
//将ref的回调形式写在外面避免更新页面在次调用render时再次触发
outInput = (c) => {
console.log(c.value)
}
changeHandle=()=>{
this.setState({
isFlag:!this.state.isFlag
})
}
state={
isFlag:true
}
render() {
const {isFlag}=this.state
return <div>
{/*通过回调的方式使用ref,页面更新会调用一次,因为render函数会再次被调用,可以将内联回调函数写在外面*/}
<input type="text" placeholder="请输入文字" ref={(c) => { this.input = c }} />
<button onClick={this.inputHandle}>确定</button>
<input type="text" placeholder="请输入文字" ref={this.outInput} />
<button onClick={this.inputHandle1}>确定1</button>
<h1 ref={(c) => { this.txt = c,console.log(c) }} >今天的天气格外的{isFlag?'炎热':'凉爽'}</h1>
<button onClick={this.changeHandle}>切换</button>
</div>
}
}
ReactDOM.render(<Input />, document.getElementById("app"))
</script>
</body>
</html>
3.3 createRef
React.createRef调用后可以返回一个容器,该容器可以存储被ref所标识的节点
首先定义一个属性,把React.createRef挂载上去
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<div id="app"></div>
<script src="../js/react.development.js"></script>
<script src="../js/react-dom.development.js"></script>
<script src="../js/babel.min.js"></script>
<script src="../js/prop-types.js"></script>
<script type="text/babel">
class Inputs extends React.Component {
inputHandle=()=>{
let {current}=this.myRef
console.log(current.value)
}
myRef=React.createRef()
render() {
return( <div>
<input type="text" placeholder="请输入文字" ref={this.myRef} />
<button onClick={this.inputHandle}>确定</button>
</div>)
}
}
ReactDOM.render(<Inputs/>, document.getElementById("app"))
</script>
</body>