2025年react获取地址栏参数(react获取页面元素)

react获取地址栏参数(react获取页面元素)下载 npm install g create react app 安装 npx create react app xxx 推荐 npm init react app xxx yarn create react app xxx class className const title index js Hello js 受控组件 多表单元素优化 非受控组件 props 父传子

大家好,我是讯享网,很高兴认识大家。



下载

npm install -g create-react-app

安装

npx create-react-app xxx 推荐

npm init react-app xxx

yarn create react-app xxx

 
  
讯享网

class => className

const title =

讯享网
 

index.js

讯享网

Hello.js

 
讯享网
 
讯享网
 
讯享网
 
讯享网
 

受控组件

讯享网

多表单元素优化

 

非受控组件

讯享网

 

props

讯享网
 
讯享网

父传子

 

子传父

讯享网

兄弟相传

 

拆分组件式

index.js :

讯享网

Child1.js :

 

Child2.js :

讯享网

类似于vue的 vuex

步骤

 

代码


讯享网

讯享网

children属性 props.children

 

props校验 约束规则 默认值

讯享网
创建 钩子函数 触发时机 作用 constructor 创建组件时 1.初始化state 2.为事件处理绑定this render 每次组件渲染时都会触发 渲染UI 注意不能调用setState() componentDidMount 组件挂载后(完成DOM渲染) 1.发送网络请求 2.DOM操作
更新 钩子函数 触发时机 作用 render 每次组件渲染都会触发 渲染UI 挂载阶段是同一个render componentDidUpdate 组件更新后 1. 发送网络请求 1. DOM操作 注意 如果要setState() 必须放在一个 if 条件中
卸载 钩子函数 触发时机 作用 componentWillUnmount 组件卸载(从页面中消失) 执行清理工作 (清除定时器)
创建 钩子函数 触发时机 作用 constructor 创建组件时 1.初始化state 2.为事件处理绑定this render 每次组件渲染时都会触发 渲染UI 注意不能调用setState() componentDidMount 组件挂载后(完成DOM渲染) 1.发送网络请求 2.DOM操作
 
讯享网
更新 钩子函数 触发时机 作用 render 每次组件渲染都会触发 渲染UI 挂载阶段是同一个render componentDidUpdate 组件更新后 1. 发送网络请求 1. DOM操作 注意 如果要setState() 必须放在一个 if 条件中



​ class App extends React.Component {

​ constructor(props) {
​ console.log(‘constructor’)
​ super(props)
​ this.state = {
​ num: 0
​ }
​ }
​ handerNum = () => {
​ this.setState({
​ num: this.state.num + 1
​ })

​ // 演示强制更新
​ // this.forceUpdate()
​ }

render() {
console.log(‘render’)
return (

























+1



)
}
}



​ class Numb extends React.Component {
​ render() {
​ console.log(‘子组件——render’)
​ return (






​ )
​ }

​ // 如果调用setState 必须放在if中
​ componentDidUpdate(prevProps) {
​ console.log(‘子组件生命周期 componentDidUpdate’)

​ // 获取DOM
​ console.log(document.getElementById(‘title’))

// this.setState({}) 报错 会递归更新










 




​ }
​ }





​ ReactDOM.render(, document.getElementById(‘root’))

​ “`



卸载 钩子函数 触发时机 作用 componentWillUnmount 组件卸载(从页面中消失)

讯享网 执行清理工作 (清除定时器) </td></tr></tbody></div> 
 

react组件复用

复用相似功能

复用什么? 1. state 2. 操作state的方法

两种方式 1. render props模式 2.高阶组件(HOC)

思路:将要复用的state和操作state的方法封装到一个组件中

讯享网
render props模式 children代替render属性
 
render props 代码优化 添加props校验
讯享网
高阶组件

使用步骤

1.创建一个函数 名称以with开头

2.指定函数参数 参数以大写字母开头 (作为要渲染的组件)

3.在函数内部创建一个类组件,提供复用的状态逻辑代码 并返回

4.在该组件中 渲染参数组件 同时将状态通过prop传递给参数组件

5. 调用该高阶组件 传入要增强的组件 通过返回值拿到增强后的组件 渲染页面

function withMouse (WrapperComponent){

class Mouse extends React.Component{}

return Mouse

}

Mouse组件的render方法中

return

 
displayName属性
讯享网
高阶组件传递props
 

setState的说明
讯享网
setState 第二个参数
 

讯享网
 
讯享网
案例
 
通过props避免不必要的重新渲染
讯享网
纯组件 (内部自动实现shouldComponentUpdate)
 
讯享网

 

npx create-react-app hkzf

yarn start

2. 组件库 antd-mobile

​ 1 打开文档 http://ant-design-mobile.antgroup.com/zh/components/button

3. 配置基础路由

​ yarn add react-router-dom

​ import { BrowserRouter as Router, Route, Routes, Link } from ‘react-router-dom’

4. 外观和样式调整

index.css

讯享网

5. 两种布局页面

  1. 有tabber页面
  2. 无tabbar页面

6. 嵌套路由

v6版本

 

v6之前版本:

讯享网

1.关闭 eslint

package.json 中修改

 


小讯
上一篇 2025-04-28 16:43
下一篇 2025-05-16 08:45

相关推荐

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容,请联系我们,一经查实,本站将立刻删除。
如需转载请保留出处:https://51itzy.com/kjqy/184841.html