• React学习总结


    1.JSX的基本使用

    1. 获取变量,插值
    2. {this.state.name}

    3. 表达式
    4. {this.state.flag? 'yes':'no'}
    5. class关键字冲突,应该使用className
    6. style={{font-size:50px;}}
    7. 原生html
    8. const rawHtml='666';
    9. const rawData={
    10. _html:rawHtml//注意必须是这种格式
    11. };
    12. <p dangerouslySetInnerHtml={rawData}>p>
    13. JSX的条件渲染
    14. render(){
    15. const blackBtn=<Button className="black-btn">black BtnButton>
    16. const whiteBtn=<Button className="white-btn">white BtnButton>
    17. //if else 语句
    18. if(this.state.color='white'){
    19. return blackBtn
    20. }
    21. else { return whiteBtn}
    22. //条件渲染
    23. return <div>
    24. { this.state.color==='white'? whiteBtn:blackBtn}
    25. div>
    26. //&&
    27. return <div>
    28. { this.state.color==='white'&&whiteBtn}
    29. div>
    30. JSX的列表循环
    31. render(){
    32. return <ul>
    33. {this.state.list.map((item,index)=>{
    34. return <li key={index.id}> index{index};title{item.title}li>
    35. })}
    36. ul>
    37. }

    2.React事件绑定this

    1. constroctur(){
    2. this.handleClick=this.handleClick.bind(this);
    3. }
    4. render(){
    5. return(
    6. <Button onClick={handleClick}>Button>
    7. )
    8. }
    9. handleClick(){
    10. xxxxxxxx
    11. }

    3.this.setState使用不可变值

    1. this.state.count++;//错误
    2. this.setState({
    3. count:this.state.count+1
    4. });
    5. 注意,在进行this.setState的操作时,不能直接对this.state.list进行push,pop,splice等,这样违反不可变值

    4.React生命周期

    1. //挂载时
    2. componentDidMount
    3. //更新时
    4. componentDidUpdate
    5. //卸载时
    6. componentWillUnMount

    5.React高级特性

    1.函数组件

        1.输入props,输出jsx

         2.没有实例,没有生命周期,没有state

         3.不能拓展其他方法

    当class组件只接收props,没有state,这时候就可以将函数组件简写为class组件

    1. //class组件
    2. class abc extends Component{
    3. constructor(props){
    4. super(props)
    5. }
    6. render(){
    7. const {list}=this.props;
    8. return <ul>{list.map((item,index)=>{
    9. return <li key={item.index}>
    10. <span>{item.title}span>li>})}
    11. ul>
    12. }
    13. }
    14. //由于该class组件只有props属性,没有state,所以可以将它简写为函数组件
    15. function abc(props){
    16. const {list}=this.props;
    17. return <ul>{
    18. list.map((item,index)=>{
    19. return <li key={item.index}>
    20. <span>{item.title}span>
    21. li>
    22. })
    23. }ul>
    24. }

    两种组件的选择:如果仅仅是输入props输出JSX,最好使用函数组件,而如果在组件中要用到生命周期函数,要用到state等,则应该使用class组件

    2.受控组件和非受控组件

    非受控组件:初始值可以使用state当中的值,但组件本身的值不和state中的值关联和绑定

    3.React Protals

    组件会按照既定层次嵌套渲染,如何让组件渲染到父组件以外?这时候就要用到Protals

    1. render(){
    2. //正常情况下
    3. return <div className="demo">{this.props.children}div>
    4. //可以使用Protals渲染到body上,fixed元素要放在body上,有更好的浏览器兼容性
    5. return ReactDOM.createProtal(
    6. <div className="demo">{this.props.children}div>,docunment.body//DOM节点
    7. )
    8. }

    4.Context

    当向子组件传递语言,主题等公共信息时,使用props太繁琐,使用redux小题大做,此时就可以使用Context

    1. 1.创建Context
    2. const ThemeContext=React.createContext('light');
    3. 2.生产Context
    4. render(){
    5. return <ThemeContext.Provider value={this.state.theme}>
    6. <button>button>
    7. <list>list>
    8. ThemeContext.Provider>
    9. }
    10. 3.使用context
    11. 如果是class组件的话,
    12. button.contextType=ThemeContext;
    13. 并且在button组件的内部
    14. render(){
    15. const theme=this.context;//使用this.context来获取theme值
    16. }
    17. 如果是函数组件的话,
    18. function list(props){
    19. <ThemeContext.Consumer>
    20. {value=><p>the theme is {value}p>}
    21. ThemeContext.Consumer>
    22. }

    5.异步组件

    如何使用异步组件

    1. const Demo=React.lazy(()=>import(./demo));
    2. class App extends Compontent{
    3. render(){
    4. <p>这是一个异步组件p>
    5. <React.Suspense fallback={<div>Loding...div>}>
    6. <Demo/>
    7. React.Suspense>
    8. }
    9. }

    6.性能优化

    shouldComponentUpdate

    1. //SCU的基本用法
    2. shouldComponentUpdate(nextProps,nextState){
    3. if(nextState.count!===this.state.count){
    4. return true//进行渲染
    5. }
    6. return false//不重复渲染
    7. }
    8. //React 默认父组件有更新,则子组件会无条件更新
    9. 通过使用SCU判断子组件是否有更新,如果有更新才进行重新渲染,这样就避免了重复渲染

    PureComponent

    在SCU当中实现了浅比较,如果第一层属性相同,则SCU返回false,不进行重复渲染,如果第一层属性不同,SCU才返回true,并且进行渲染

    1. class demo extends React.pureComponent{
    2. //相当于增加了SCU(){浅比较}
    3. }

    memo

    1. class demo extends React.Component{
    2. }
    3. function preNext(preProps,nextProps){
    4. //比较函数
    5. }
    6. export default React.memo(demo,preNext);

    immutable.js

    const map1=immutable.Map({a:1,b:2,c:3});

    const map2=map1.set('b',50);

    map1.get('b');//2

    map2.get('b');//50

    7.关于组件公共逻辑的抽离

    高阶组件(HOC)

    1. //高阶组件不是一种功能,而是一种模式
    2. const HOCFactory=(Component)=>{
    3. class HOC extends React.Component{
    4. constroctur(props){
    5. super(props);}
    6. //在此定义多个组件的公共逻辑
    7. }
    8. render(){
    9. return <Component {...this.props}/>//返回拼装的结果
    10. }
    11. return HOC
    12. };
    13. const EnhancedComponent1=HOCFactory(WarpedComponent1);
    14. const EnhancedComponent2=HOCFactory(WarpedComponent2);

    8.Render Props

    Render Props的核心思想

    通过一个函数将class组件的state作为props传递给纯函数组件

    1. class Factory extends Component{
    2. constroctur(){
    3. this.state={
    4. /*state 即多个组件的公共逻辑的数据*/}
    5. a:555,
    6. b:666
    7. }
    8. render(){
    9. <div>{this.props.render(this.state)}div>}
    10. }
    11. const App=()=>{
    12. <Factory render={
    13. (props)=><p>{props.a},{props.b}/>
    14. }

    9.Redux单向数据流

    1.dispatch(action)

    2.reducer返回NewState

    3.subscribe触发通知

    10.异步action

    使用中间件

    redux-thunk

    redux-saga

    redux-promise

    11.React-Router路由模式

    1.哈希模式 如http://abc/#/user/20

    2.H5 history模式 如http://abc/user/10

    后者需要server端支持,因此无特殊要求选择前者

    1. import React from 'react'
    2. import {
    3. /哈希路由/
    4. HashRouter as Router,
    5. Switch,
    6. Route
    7. } from 'react-router-dom'
    8. import {
    9. /h5 history路由/
    10. BrowserRouter as Router,
    11. Sitch,
    12. Route
    13. } from 'react-router-dom'
    14. function routerComponent(){
    15. return(
    16. <Router>
    17. <Switch>
    18. <Route path="/">
    19. <Home/>
    20. Route>
    21. <Route path="/project/:id">
    22. <Project/>
    23. Route>
    24. <Route path="*">
    25. <Not Found/>
    26. Route>
    27. Switch>
    28. Router>
    29. )
    30. }

    路由跳转

    1.使用Link标签实现路由跳转

    1. import React from 'react'
    2. import {Link,useParams} from 'react-router-dom'
    3. function Project(){
    4. const {id}=useParams();
    5. return(
    6. <div link="/">首页div>)
    7. }

    2.使用js实现路由跳转

    1. import React from 'react'
    2. import { useHistory } from 'react-router-dom'
    3. function Trash(){
    4. let history=useHistory();
    5. function handleClick(){
    6. history.push('/');
    7. }
    8. return (
    9. <div>
    10. <Button type="primary" onClick={handleClick}>回到首页<Button/>
    11. div>
    12. )
    13. }

    12.React-Router路由配置(懒加载)

    1. import {BrowserRouter as Router ,Switch,Route} from 'react-dom-router'
    2. import React ,{Suspense,lazy} from 'react'
    3. const Home=lazy(()=>import('./routes/Home'));
    4. const About=lazy(()=>import('./routes/About'));
    5. const App=()=>{
    6. <Router>
    7. <Suspense fallback={<div>Loading...div>}>
    8. <Switch>
    9. <Route exact path="/" component={Home}/>
    10. <Route path="/about" component={About}/>
    11. Switch>
    12. Suspense>
    13. Router>
    14. };

  • 相关阅读:
    TF-IDF在现代搜索引擎优化策略中的作用
    JAVA大学生社团活动管理系统计算机毕业设计Mybatis+系统+数据库+调试部署
    iMazing 2023年最新苹果手机怎么备份恢复照片
    Mybatis插件功能
    Spring框架(十一):手动实现一个@Component,讲一讲Spring的工厂后处理器
    Spark 离线开发框架设计与实现
    模仿苹果生态系统,谷歌2022年将致力于跨平台设备整合
    Linux shell 内建命令
    canal1.1.7实战
    protobuf 反射使用总结
  • 原文地址:https://blog.csdn.net/weixin_44858541/article/details/125765517