一、state的理解
复杂组件和简单组件的区分标准:有无状态(state) 组件的状态(state)里存放着数据,数据的改变就会驱动页面的展示。
1.state是组件对象最重要的属性,值是对象(可以包含多个key-value的组合)。 2.组件被成为“状态机”,通过更新组件的state来更新对应页面的展示。
注意:
1.组件中render的this为组件实例对象。
2.组件自定义的方法中this为undefined,如何解决?
1)强制绑定this。通过函数对象的bind();
2)箭头函数
3)状态数据,不能直接修改或更新,要使用setState
二、初始化state
初始化state需要引入constructor构造器
<!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>组件实例对象的三大属性1-state(初始化state)</title>
</head>
<body>
<div id="test"></div>
<script crossorigin src="https://unpkg.com/react@16/umd/react.development.js"></script>
<script crossorigin src="https://unpkg.com/react-dom@16/umd/react-dom.development.js"></script>
<script src="https://unpkg.com/babel-standalone@6/babel.min.js"></script>
<script type="text/babel">
class Weather extends React.Component {
constructor(props) {
super(props);
this.state = { isHot: false }
}
render() {
const { isHot } = this.state
return (
<h1>今天天气{isHot ? '炎热' : '寒冷'}</h1>
)
}
}
ReactDOM.render(<Weather />, document.querySelector('#test'))
</script>
</body>
</html>
三、事件绑定
<!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>react 事件绑定</title>
</head>
<body>
<div id="app"></div>
<script crossorigin src="https://unpkg.com/react@16/umd/react.development.js"></script>
<script crossorigin src="https://unpkg.com/react-dom@16/umd/react-dom.development.js"></script>
<script src="https://unpkg.com/babel-standalone@6/babel.min.js"></script>
<script type="text/babel">
class Demo extends React.Component {
constructor(props) {
super(props)
this.state = { isHot: true }
this.click3 = this.click3.bind(this)
}
render() {
return (
<div>
<h2>内容显示:{this.state.isHot ? '炎热' : '寒冷'}</h2>
<button onClick={this.click1}>点击1(this指向为undefined)</button>
<br />
<button onClick={this.click2}>点击2(this指向为Demo组件实例对象)</button>
<br />
<button onClick={this.click3}>点击3(修改内容显示-通过bind改变this指向)</button>
<br />
<button onClick={this.click4}>点击4(修改内容显示-通过箭头函数)</button>
</div>
)
}
click1() {
console.log(this)
}
click2 = () => {
console.log(this)
}
click3() {
console.log(this);
const { isHot } = this.state;
this.setState({ isHot: !isHot })
}
click4 = () => {
console.log(this);
const { isHot } = this.state;
this.setState({ isHot: !isHot })
}
}
ReactDOM.render(<Demo />, document.querySelector('#app'))
</script>
</body>
</html>
四、简写 state(使用箭头函数定义事件方法)
<!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>简写state(建议使用箭头函数)</title>
</head>
<body>
<div id="test"></div>
<script crossorigin src="https://unpkg.com/react@16/umd/react.development.js"></script>
<script crossorigin src="https://unpkg.com/react-dom@16/umd/react-dom.development.js"></script>
<script src="https://unpkg.com/babel-standalone@6/babel.min.js"></script>
<script type="text/babel">
class Weather extends React.Component {
constructor(props) {
super(props);
}
state = { isHot: false };
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.querySelector('#test'));
</script>
</body>
</html>
以上就是React 组件(类式)实例对象的三大属性1-state的内容,请大家关注《React 全家桶》专栏。 我会将自己平时项目中常见的问题以及笔试面试的知识在CSDN与大家分享,一起进步,加油。
|