(1.1)react组件es5
一个函数就是一个组件, 这个函数需要有一个return()
import React from 'react';
export default function() {
return(
<div>
<h4>es5组件</h4>
<p>
hello react
</p>
</div>
);
}
(1.2)react组件es6
- 需要提供constructor并执行super()
- 需要提供一个render方法 ```javascript import React from ‘react’;
class Demo extends React.Component{ constructor() { super(); }
// 需要提供一个render方法
render() {
return (
<div>
<h4>es6 组件</h4>
<p>
hello react
</p>
</div>
)
}
}
export default Demo;
<a name="6fRJ3"></a>
## (2)react静态页面
```javascript
import React from 'react';
export default function() {
return(
<div>
<h4>静态页面</h4>
<div className="box" style={{border:'1px solid',color:'red'}}>
阿斯顿发是的发送到发斯蒂芬阿瑟发斯蒂芬
</div>
</div>
)
}
(3)react生命周期
import React, { Component } from "react";
class Demo extends Component {
constructor() {
super();
}
render() {
return <div>生命周期函数</div>
}
componentWillMount() {
console.log("组件将要挂载");
}
componentDidMount() {
console.log("组件已经挂载");
}
componentWillReceiveProps(newProps) {
console.log("组件将接收属性数据");
}
shouldComponentUpdate(nextProps, nextState) {
// 此生命周期用来优化性能
return true;
}
componentWillUpdate(nextProps, nextState) {
console.log("组件将要更新");
}
componentDidUpdate(prevProps, prevState) {
console.log("组件已经更新完毕");
}
componentWillUnmount() {
console.log("组件将要卸载");
}
}
export default Demo;
(4)jsx语法和注释
import React from 'react';
export default function() {
let person = {
username:'法外狂徒',
page: 100
}
/**
* jsx语法
* 1. 遇到<>,看做html来进行解析
* 2. 遇到{},看做是js来进行解析
* 3. 注释{/* */}
*/
return(
<div>
<p>姓名: {person.username}</p>
<p>年龄: {person.age}</p>
</div>
)
}
(5.1)事件绑定
import React from "react";
class Demo extends React.Component {
constructor() {
super();
// 让test函数的this永远指向Demo实例
this.test = this.test.bind(this);
}
test() {
alert("哈哈哈哈哈哈哈");
}
render() {
return (
<div>
<button onClick={this.test}>点我啊</button>
</div>
);
}
}
export default Demo;
(5.2)事件绑定
import React from 'react';
class Demo extends React.Component{
constructor() {
super();
this.test = this.test.bind(this);
}
test(data) {
console.log(data);
}
render() {
return(
<div>
<button onClick={()=>{this.test('111')}}>张三</button>
<button onClick={()=>{this.test('222')}}>李四</button>
<button onClick={()=>{this.test('333')}}>王五</button>
</div>
)
}
}
export default Demo;
(6)列表渲染
import React from 'react';
class Demo extends React.Component{
constructor() {
super();
}
render() {
let list = [
{username:'zs1',age:20,id:"01"},
{username:'zs2',age:21,id:"02"},
{username:'zs3',age:22,id:"03"},
{username:'zs4',age:23,id:"04"},
]
return(
<ul>
{
list.map((item,index)=> {
return (
<li key={item.id}>
{index+1}. <span>用户名: {item.username}</span> <span>年龄: {item.age}</span>
</li>
)
})
}
</ul>
)
}
}
export default Demo;
(7)条件渲染
import React from 'react';
export default function() {
let isLogin = true;
return (
<div>
<h4>条件渲染</h4>
{ isLogin? <p>138****0000</p>:
<p>还没登录,<button>立即登录</button></p>
}
</div>
)
}
(8)state和setState
import React from 'react';
class Demo extends React.Component{
constructor() {
super();
this.state = {
isLogin: false
}
this.login = this.login.bind(this);
}
login() {
// 修改state里的状态
this.setState({
isLogin: true
});
}
render() {
console.log('执行render方法');
let {isLogin} = this.state;
return (
<div>
<h4>条件渲染</h4>
{ isLogin? <p>138****0000</p>:<p>还没登录,<button onClick={this.login}>立即登录</button></p> }
</div>
)
}
}
export default Demo;
(8.2)state和性能优化
- 只要执行setState, render函数默认会执行
- 使用shouldUpdateComponent进行性能优化, 只有当状态发生改变的时候在执行render函数 ```javascript import React from ‘react’;
class Demo extends React.Component{ constructor() { super(); this.state = { show: false } this.change1 = this.change1.bind(this); this.change2 = this.change2.bind(this); }
change1() {
// 修改state里的状态
this.setState({
show: !this.state.show
});
}
change2() {
// 修改state里的状态
this.setState({
show: true
});
}
/**
* 优化性能
* @param {*} nextProps 下一个属性(新属性), nextState
* @param {*} nextState 下一个state(新的状态)
* @returns
*/
shouldComponentUpdate(nextProps, nextState) {
// 新的状态和旧的状态的值相同时, 不重新渲染页面
if (this.state.show === nextState.show) {
// 返回false不重新渲染页面
return false;
} else {
// 返回true重新渲染页面
return true;
}
}
render() {
console.log('重新渲染')
let {show} = this.state;
return (
<div>
<h4>state和render</h4>
<p>
<button onClick={this.change1}>btn1</button>
<button onClick={this.change2}>btn2</button>
</p>
{
show? <div>
<p>sdfasdfasdfasdfasdfasdfasdf</p>
<p>sdfasdfasdfasdfasdfasdfasdf</p>
<p>sdfasdfasdfasdfasdfasdfasdf</p>
</div>:''
}
</div>
)
}
}
export default Demo;
<a name="OlAdG"></a>
## (9.1)表单输入
通过setState来显示用户的输入
```javascript
import React from 'react';
class Demo extends React.Component{
constructor() {
super();
this.state = {
username: ''
}
this.changeUsername = this.changeUsername.bind(this);
}
changeUsername() {
this.setState({
username: event.target.value
})
}
render() {
return(
<div>
<h4>表单输入</h4>
<input type="text" value={this.state.username} onChange={this.changeUsername}/>
</div>
)
}
}
export default Demo;
(9.2)多个输入框
import React from 'react';
class Demo extends React.Component{
constructor() {
super();
this.state = {
username: '',
password:''
}
this.change = this.change.bind(this);
}
change() {
// 获取输入的dom节点
let target = event.target;
// input标签上定义的name的值
let name = target.name;
this.setState({
[name]: target.value
})
}
render() {
return(
<div>
<h4>表单输入</h4>
<input placeholder="用户名" type="text" name="username" value={this.state.username}
onChange={this.change}/>
<input placeholder="密码" type="text" name="password" value={this.state.password}
onChange={this.change}/>
</div>
)
}
}
export default Demo;
(10)获取真实的 DOM 节点
import React from "react";
class Demo extends React.Component {
constructor() {
super();
this.handleClick = this.handleClick.bind(this);
}
handleClick() {
// 让输入框获得焦点
this.refs.inp.focus();
}
render() {
return (
<span onClick={this.handleClick} style={{background:'#fff',display:'inline-block',width:'100px',height:'30px',border:'1px solid'}}>
<input ref="inp" type="text" style={{width:'80%', border: 'none', outline: 'none'}}/>
</span>
);
}
}
export default Demo;
(11)父子组件通信
// 父组件
import React from 'react';
import Son from './Son'
class Father extends React.Component{
constructor() {
super();
this.getMsg = this.getMsg.bind(this);
}
// 接收子组件传过来的数据
getMsg(data) {
console.log(data);
}
render() {
return(
<div>
<h3>父组件</h3>
<hr/>
{/* 给子组件传递属性和方法 */}
<Son msg="hello son" getMsg={this.getMsg}/>
</div>
)
}
}
export default Father;
// 子组件
import React from 'react';
class Son extends React.Component{
constructor() {
super();
}
render() {
let {msg,getMsg} = this.props;
return(
<div>
{msg}<br/>
{/* 调用父组件传过来的方法,并传递数据 */}
<button onClick={()=>{getMsg('哈哈哈哈哈')}}>发送消息</button>
</div>
)
}
}
export default Son;