react基本用法总结

函数组件内部的状态管理

1.对于函数组件内部的状态管理 钩子 常用的是useState ,可以通过过useState 初始化状态和改变状态的方法 具体如下 对于useState的dispatch改变状态方法 最佳使用方法是传入回调方法进行赋值 这样才能保证每次修改后都会及时更新

1)对于数组和对象 直接修改key /改变数组原始值的方法 类似于push... 并不能触发更新,要直接创建一个新对象进行set 注意:不论是useState 或 setState(类组件的状态更新)都是异步触发的 并且对于多次状态更新 会合并为一次 (目前是为了优化渲染性能)所以当同时执行多次时 只会使用最后一次的结果 如果需要在短时间内多次改变状态 可以使用函数式更新

import React, { useState } from 'react';

function Counter() {
    const [count, setCount] = useState(0);
    const [lists,setLists] = useState(['apple','orange','pineapple','bananan'])
    function addLists(){
        
        setLists([...lists,'test123'])
        setLists([...lists,'test123456'])//只有这条会生效 会直接覆盖上面效果
        // 使用函数式更新写法 会正常添加两条
        setLists((lists)=>{return [...lists,'test123']})
        setLists((lists)=>{return [...lists,'test123456']})
        
    }
  return (
    <div>
        {lists.map((item)=>(<p key={item}>{item}</p>))}
        <button onClick={addLists}></button>
        <p>Count: {count}</p>
        <button onClick={() => setCount((count)=>count + 1)}>Increment</button>
    </div>
  );
}

export default Counter;

对于类组件 内部的状态管理 则是直接使用this.state 在constructor里进行初始化 通过this.setState 在原型方法里进行状态改变 具体如下 (注意 想要在原型方法里直接使用 this 只能使用 箭头函数 或着 通过bind 对 this指向进行绑定

class couter extends Component{
    constructor(props){
        // 挂载前 初始化
        super(props)
        // 类组件里的状态初始化 改变状态 --> this.setState
        this.state={
            count:0,
            hasColor:false,
            lists:[
                {
                    name:'book1'
                },
                {
                    name:'book2'
                },
                {
                    name:'book3'
                }
            ]
        }
        // 对原型里的this 进行绑定
        // this.increment =this.increment.bind(this);
    }
    increment=()=>{
        // 1. 不能直接使用 this 想要使用 this 需要在constructor 里面对 this进行绑定 2 .或者直接使用 箭头函数        console.log('sdtate=====',this.state.count)
        console.log('state======>',this.state.count)
        // debugger
        this.setState({count:this.state.count+1})
    }
}
元素的事件绑定

2.对于元素的事件绑定的两种方法( 直接在行内调用 / 通过定义的函数进行调用 )

1)行内调用的写法(见onClick)

render(){
 return (
                <>
                    <h3>other Components</h3>
                    <button onClick={()=>{this.setState({hasColor:!this.state.hasColor})}}>hasColor</button>
                    {this.state.hasColor?<h3>222222</h3>:<h2>333333</h2>}
                </>
            )
}

2)定义的函数调用的写法(见onClick)

 return (
                <>
                <h1>hello {this.props.name}</h1>
                <span>{this.state.count}</span>
                {/* 调用封装好的方法 */}
                <button onClick={this.increment}>upCount</button>
                {this.state.lists.map((item)=>(<p key={item.name.toString()}>{item.name}</p>))}
                </>
                )
对于列表的渲染

在render里通过map遍历进行渲染 同vue v-for一样 需要绑定key(切记 react里再html里所有变量及动态绑定的都是通过{})具体写法如下

{this.state.lists.map((item)=>(<p key={item.name.toString()}>{item.name}</p>))}
对于条件判断的切换

可以使用if 或者 三目运算

具体情况看 逻辑的复杂程度 代码如下(由于两个状态的逻辑都比较复杂 所以使用if判断)

render(){
        //
        if(this.props.isLogin){
            return (
                <>
                <h1>hello {this.props.name}</h1>
                <span>{this.state.count}</span>
                {/* 调用封装好的方法 */}
                <button onClick={this.increment}>upCount</button>
                {this.state.lists.map((item)=>(<p key={item.name.toString()}>{item.name}</p>))}
                </>
                )
        }else{
            return (
                <>
                    <h3>other Components</h3>
                    <button onClick={()=>{this.setState({hasColor:!this.state.hasColor})}}>hasColor</button>
                    {this.state.hasColor?<h3>222222</h3>:<h2>333333</h2>}
                </>
            )
        }
        
    }
对于父子组件通信

不同于 vue props emit的传参和触发事件 react直接通过props 传递参数 和方法 (子组件直接调用传递的方法即可触发父组件里的绑定事件)

子组件需要再constructor里通过 super(props) 然后才能在下面进行调用 具体代码如下

父组件
render(){
    return (
    <Couter name="wolrd" isLogin={isLogin} />
    )
}
子组件
constructor(props){
    super(props)
}
render(){
    // 通过this.props 获取
    if(this.props.isLogin){

    }
}

类似于 vue react也有 props验证 但是写法有所不同 具体如下(从react15版本 props验证就专门移到了 prop-types 库)

import propTypes from 'prop-types'; 
class couter extends Component{
    // props验证
    static propTypes={
        isLogin:propTypes.bool,
        name:propTypes.string
    }
    constructor(props){
        super(props)
    }
}

还有很多的写法具体可见菜鸟教程

关于react性能优化的几个api reack hooks

useMemo 类似于 vue computed计算属性 

React.memo 用于缓存组件()

在react中只要父组件更新 子组件会递归更新 如果大量组件嵌套会导致性能缓慢 由于 memo 只注重props的变化 其他变量的变化不会触发组件的重新渲染 

useCallback 用于对函数进行性能优化

1)在函数组件中 生命函数事件 并将其作为子组件的props 每次父组件的渲染都会触发 事件的定义 从而触发子组件的渲染  通过useCallback则可以避免这一情况

2)或在在函数被频繁调用 在昂贵计算中 例如循环 传入依赖项 可在指定依赖变化时才触发函数 从而提高性能

useEffect

综合了 componentDidmount  componentDidupdate componentWillunmount 的功能 如果不传入依赖 则立即执行一次 如果传入依赖 则依赖变化时执行

关于 生命周期函数钩子

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值