[React]面向组件编程

1. 定义组件

- 函数式定义(简单组件),使用function定义

import React from 'react';
import ReactDOM from 'react-dom/client';function App() {return (<button onClick={handleClick}>click</button> // 直接把方法handleClick赋值给onClick)function handleClick() {alert('click')}
}
export default App; // 其他组件引用时需要export出去const root = ReactDOM.createRoot(document.getElementById('root'));
root.render(<App />);

- 类式定义(复杂组件),使用class定义

import React from 'react';
import ReactDOM from 'react-dom/client';class App extends React.Component {render() {return (<button onClick={this.handleClick}>click</button>  // this代表在App的实例对象上赋值handleClick)}handleClick() {alert('click')}
}
export default App; // 其他组件引用时需要export出去const root = ReactDOM.createRoot(document.getElementById('root'));
root.render(<App />);

2. state,设置组件初始值

- 原始定义,需要在constructor中定义state和绑定方法handleClick的this

class Weather extends React.Component {// 创建构造器,类似ruby的initializeconstructor(props) {// 构造器是否接收props,是否传递给super,取决于是否希望在构造器中通过this访问props// 如果是 super(),则 console.log(this.props)为undefined, console.log(props)就是传进来的props// 如果是 super(props),则 console.log(this.props)为Weather的实例super(props) // 调用父类React.Component的构造器this.state = { isHot: true, wind: 'big' }this.handleClick = this.handleClick.bind(this) // 给handleClick绑定this}render() {const isHot = this.state.isHotreturn (<h1 onClick={this.handleClick}>Today is very {isHot ? 'hot' : 'cold'}, wind: {this.state.wind}</h1>)}handleClick() {this.setState({ isHot: !this.state.isHot, wind: this.state.wind !== 'big' ? 'big' : 'small' })}
}

- 简化后在类中直接给state和handleClick赋值,就直接把属性和方法绑定到Weather的实例上了

class Weather extends React.Component {// 初始化state = { isHot: true, wind: 'big' }// 渲染render() {const isHot = this.state.isHotreturn (<h1 onClick={this.handleClick}>Today is very {isHot ? 'hot' : 'cold'}, wind: {this.state.wind}</h1>)}// 自定义方法handleClick = () => {this.setState({ isHot: !this.state.isHot, wind: this.state.wind !== 'big' ? 'big' : 'small' })}
}

3. props,从组件外部给组件定义中传值

- 函数式组件使用props

- 函数式组件使用props
```typescript
import React from 'react';
import ReactDOM from 'react-dom/client';
import PropTypes from 'prop-types';function Person(props){const { name, age, sex } = propsreturn (<ul><li>name: {name}</li><li>age: {age}</li><li>sex: {sex}</li></ul>)
}
// 限制属性
Person.propTypes = {name: PropTypes.string.isRequired,age: PropTypes.number,sex: PropTypes.string,speak: PropTypes.func
}
// 设置属性默认值
Person.defaultProps = {age: 18,sex: 'female'
}function speak() {console.log('speak')
}const root = ReactDOM.createRoot(document.getElementById('root'));
const p = { name: 'Tom' }
root.render(<React.StrictMode>{/* <Person name={p.name} age={p.age} sex={p.sex} speak={speak} /> */}<Person {...p} speak={speak} /></React.StrictMode>
);

- 类式组件使用props

import React from 'react';
import ReactDOM from 'react-dom/client';
import PropTypes from 'prop-types';class Person extends React.Component {render() {const { name, age, sex } = this.propsreturn (<ul><li>name: {name}</li><li>age: {age}</li><li>sex: {sex}</li></ul>)}state = { isHot: true } // 这是给组件实例对象赋值// 限制属性 前面加 static 是给组件类赋值static propTypes = {name: PropTypes.string.isRequired,age: PropTypes.number,sex: PropTypes.string,speak: PropTypes.func}// 设置属性默认值static defaultProps = {age: 18,sex: 'female'}
}function speak() {console.log('speak')
}const root = ReactDOM.createRoot(document.getElementById('root'));
const p = { name: 'Tom' }
root.render(<React.StrictMode>{/* <Person name={p.name} age={p.age} sex={p.sex} speak={speak} /> */}<Person {...p} speak={speak} /></React.StrictMode>
);

4. refs,获取其他html元素的dom,相当于$(‘#xx’)

- 字符串形式ref

class Demo extends React.Component {render() {return (<><input ref="input1" type="text" /><button onClick={this.tipLeftInput}>Click me</button><input ref="input2" onBlur={this.tiprightInput} type="text" /></>)}tipLeftInput = () => {alert(this.refs.input1.value);}tiprightInput = () => {alert(this.refs.input2.value);}
}

- 回调形式ref

class Demo extends React.Component {render() {return (<><input ref={(currentNode) => { this.input1 = currentNode }} type="text" />{/* <input ref={currentNode => this.input1 = currentNode} type="text" />  简写 */}<button onClick={this.tipLeftInput}>Click me</button><input ref={(currentNode) => { this.rightInput = currentNode }} onBlur={this.tiprightInput} type="text" /></>)}tipLeftInput = () => {alert(this.input1.value);}tiprightInput = () => {alert(this.rightInput.value);}
}
  • ref回调函,如果是内联函数方式定义的,在更新过程中(更改state重新渲染render)会被执行两次,第一次传入参数null,第二次传入参数为DOM, 可以将ref回调函数定义为class的绑定函数避免上述问题。
    在这里插入图片描述

- createRef api创建ref , 较麻烦

class Demo extends React.Component {render() {return (<><input ref={this.myRef1} type="text" /><button onClick={this.tipLeftInput}>Click me</button><input ref={this.myRef2} onBlur={this.tiprightInput} type="text" /></>)}myRef1 = React.createRef()myRef2 = React.createRef()tipLeftInput = () => {console.log(this.myRef1.current.value);}tiprightInput = () => {console.log(this.myRef2.current.value);}
}

5. 事件处理

- button要获取input的值,使用ref

class Demo extends React.Component {render() {return (<><input ref={this.myRef1} type="text" /><button onClick={this.tipLeftInput}>Click me</button></>)}myRef1 = React.createRef()tipLeftInput = () => {console.log(this.myRef1.current.value);}
}

- input获取本身DOM的值,使用e.target获取dom

class Demo extends React.Component {render() {return (<><input onBlur={this.tiprightInput} type="text" /></>)}tiprightInput = (e) => {console.log(e.target.value);}
}

6. 非受控组件和受控组件

- 非受控组件,使用ref取值,不建议使用

class Login extends React.Component {render() {return (<form onSubmit={this.submit}><input type="text" name="username" ref={ref => this.username = ref} /><input type="text" name="password" ref={ref => this.password = ref} /><input type="submit" name="" /></form>)}submit = (e) => {e.preventDefault();alert(`username: ${this.username.value}, password: ${this.password.value}`)}
}

- 受控组件,使用state取值,建议使用

class Login extends React.Component {state = {username: '',password: ''}render() {return (<form onSubmit={this.submit}><input type="text" name="username" onChange={this.saveUsername} /><input type="text" name="password" onChange={this.savePassword} /><input type="submit" name="" /></form>)}saveUsername = (e) => {this.setState({ username: e.target.value })}savePassword = (e) => {this.setState({ password: e.target.value })}submit = (e) => {e.preventDefault();alert(`username: ${this.state.username}, password: ${this.state.password}`)}
}

7.高阶函数

-上面的代码重构,onChange 调用的是saveFormdate方法return的返回值,是个函数
必须把一个函数交给onChange

class Login extends React.Component {state = {username: '',password: ''}render() {return (<form onSubmit={this.submit}><input type="text" name="username" onChange={this.saveFormdate('username')} /><input type="text" name="password" onChange={this.saveFormdate('password')} /><input type="submit" name="" /></form>)}saveFormdate = (params) => {return (e) => {this.setState({ [params]: e.target.value })}}submit = (e) => {e.preventDefault();alert(`username: ${this.state.username}, password: ${this.state.password}`)}
}

8. 生命周期,回调函数或者叫钩子函数

class Life extends React.Component {// 构造器constructor(props) {console.log('constructor')super(props)this.state = { num: 0 }}// 组件挂载前调用componentWillMount() {console.log('componentWillMount')}// 询问是否更新组件调用shouldComponentUpdate() {console.log('shouldComponentUpdate')return true // 默认返回true}// 组件更新前调用componentWillUpdate() {console.log('componentWillUpdate')}// 组件挂载,初始化渲染、状态更新后调用render() {console.log('render')return (<><h1 style={{ opacity: this.state.opacity }}>当前数字: {this.state.num}</h1><button onClick={this.add}>数字+1</button><button onClick={this.unmount}>卸载组件</button></>)}// 组件更新后调用componentDidUpdate() {console.log('componentDidUpdate')}// 组件挂载后调用,调用一次componentDidMount() {console.log('componentDidMount')}// 组件卸载前调用componentWillUnmount() {console.log('componentWillUnmount')}// 数字+1调用add = () => {const { num } = this.statethis.setState({ num: num + 1 })}// 卸载组件调用unmount = () => {root.unmount();}
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.hqwc.cn/news/2466.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Elasticsearch-01篇(单机版简单安装)

Elasticsearch-01篇&#xff08;单机版简单安装&#xff09; 1. 前言1.1 关于 Elastic Stack 2. Elasticsearch 的安装&#xff08;Linux&#xff09;2.1 准备工作2.1.1 下载2.1.2 解压&#xff08;启动不能用root&#xff0c;所以最好此处换个用户&#xff09; 2.2 修改相应的…

OpenStack(T版)——网络(Neutron)服务介绍与安装

文章目录 OpenStack(T版)——网络(Neutron)服务介绍与安装安装和配置(controller)准备(1)创建数据库(2)加载admin user的环境变量(3)创建服务凭证 配置Neutron网络服务组件(1)安装软件(2)配置服务器组件(3)配置Layer 2 (ML2)plug-in模块(4)配置桥接代理(5)配置内核(6)配置DHCP代…

JS知识点汇总(七)--数据类型

1. JavaScript中的简单数据类型有哪些&#xff1f; 1、概述 JS 中有六种简单数据类型&#xff1a;undefined、null、boolean、string、number、symbol ES10中的新特性 BigInt (任意精度整数)&#xff0c;目前还处于stage-4阶段&#xff0c;不出意外即将成为js的第七种基本数据…

多元函数微分

1-7 8&#xff0c;梯度 多元函数梯度&#xff1a; 方向导数是梯度在L方向上的投影 梯度方向是f增长最快的方向 9&#xff0c;极值点处若存在偏导&#xff0c;则该点为驻点&#xff0c;该点处的各偏导值为0 10&#xff0c; 二阶偏导和极值 二元函数&#xff1a; 多元函数的…

企业电子招投标采购系统之项目说明和开发类型源码

项目说明 随着公司的快速发展&#xff0c;企业人员和经营规模不断壮大&#xff0c;公司对内部招采管理的提升提出了更高的要求。在企业里建立一个公平、公开、公正的采购环境&#xff0c;最大限度控制采购成本至关重要。符合国家电子招投标法律法规及相关规范&#xff0c;以及审…

哪个爬虫库用的最多?

在Python中&#xff0c;最常用的爬虫库是requests和BeautifulSoup。requests库用于发送HTTP请求和处理响应&#xff0c;而BeautifulSoup库用于解析HTML文档。这两个库通常结合使用&#xff0c;用于爬取网页内容并提取所需的数据。其他常用的爬虫库还包括Scrapy、Selenium等。 常…

kkfileview部署使用

1.gitee下载源码 kkFileView: 使用spring boot打造文件文档在线预览项目解决方案&#xff0c;支持doc、docx、ppt、pptx、wps、xls、xlsx、zip、rar、ofd、xmind、bpmn 、eml 、epub、3ds、dwg、psd 、mp4、mp3以及众多类文本类型文件在线预览 2.去掉cad 3.替换水印字体为免费…

构建交互式数据框架:使用Gradio的Dataframe模块

❤️觉得内容不错的话&#xff0c;欢迎点赞收藏加关注&#x1f60a;&#x1f60a;&#x1f60a;&#xff0c;后续会继续输入更多优质内容❤️ &#x1f449;有问题欢迎大家加关注私戳或者评论&#xff08;包括但不限于NLP算法相关&#xff0c;linux学习相关&#xff0c;读研读博…

视频解说小程序看点小程序搭建上线,流量主对接实现广告收益

什么是视频解说小程序&#xff1f; 影视剪辑和解说&#xff0c;我们都知道有这类的抖音号&#xff0c;这时候就用到我们小程序了&#xff0c;流量主产生了收益。把视频解说上传到小程序&#xff0c;设置为广告观看&#xff0c;这样引导用户去小程序看&#xff0c;就产生一个广告…

配置鼠标右键菜单功能 :一键csv转excel

配置右键菜单功能 &#xff1a;一键csv转excel 无需点开文件&#xff0c;双击即可以生成新的excel文件 步骤&#xff1a; 1、配置Python&#xff0c;安装依赖库 pip install openpyxl pip install pandas2、创建Python文件 csv_to_excel.py # -*- coding:utf-8 -*- impor…

基于Java+SSM+Vue的高校校园点餐系统设计与实现

博主介绍&#xff1a; 大家好&#xff0c;我是一名在Java圈混迹十余年的程序员&#xff0c;精通Java编程语言&#xff0c;同时也熟练掌握微信小程序、Python和Android等技术&#xff0c;能够为大家提供全方位的技术支持和交流。 我擅长在JavaWeb、SSH、SSM、SpringBoot等框架…

2023年城市分站系统源码采用php语言

系统源码介绍&#xff1a; 本系统采用了php语言&#xff0c;根据现有了城市分布&#xff0c;包含了省市区&#xff0c;划分&#xff0c;具备了高级少选功能&#xff0c;按照每个市级城市为分站点&#xff0c;和主站点同样的模式。 安装方法&#xff1a; 快速创建几百个城市分…