react学习笔记-03

目录

请求数据

axios

安装

使用

fetch

安装

使用

React.Fragment

懒加载

Portal

高阶函数 | 高阶组件


请求数据


axios

安装

npm i axios

使用

import Axios from 'axios';

Axios.post('http://localhost:7070/list',{},{
            headers:{
                "Content-Type":"application/json"
            }
        }).then((res)=>{
            console.log(res);
            this.setState({
                list:res.data
            })
        }).catch((err)=>{
            console.log(err);
        })

Vue axios 前端接口请求封装(基础)_哔哩哔哩_bilibili

拦截器 | Axios 中文文档 | Axios 中文网Axios API | Axios 中文文档 | Axios 中文网

vue项目中axios的封装_阳光-CSDN博客_vue项目axios封装

fetch

安装

npm i whatwg-fetch

使用

        const that = this;
        fetch('http://localhost:7070/list',{
            method: 'POST',
            headers: {
                'Content-Type': 'application/json'
            }
        }).then(function(response) {
                return response.json()
            }).then(function(json) {
                // 这里才能拿到数据
            that.setState({
                list:json
            })
        }).catch(function(ex) {
            console.log('parsing failed', ex)
        })

whatwg-fetch - npm

React.Fragment


<React.Fragment>
  <li>1</li>
  <li>2</li>
  <li>3</li>
</React.Fragment>

// 简写
<>
  <li>1</li>
  <li>2</li>
  <li>3</li>
</>

//编译结果:
  <li>1</li>
  <li>2</li>
  <li>3</li>

懒加载


const StudentList = React.lazy(()=>import('./StudentList'));

<StudentList/>

//或者
import {Suspense}from 'react';

        <Suspense fallback={<div>Loading</div>}>
            <StudentList/>
        </Suspense>

Portal


 Portal 提供了一种将子节点渲染到存在于父组件以外的 DOM 节点的优秀的方案。

// 模态框,子组件
funtion ZheZhao(){
	return <div  className='zheZhao'>
                   展示信息
	       </div>
}

exports default ZheZhao;

//父组件
function StudentList(){
	return (<>
                 <button>添加</button>
	        </>)
}
ReactDOM.createPortal(<ZheZhao>, document.body)

react portal - 简书

高阶函数 | 高阶组件


HOF : higher order function 高阶函数
定义:一个函数可以接收另一个函数作为参数,这种函数就称之为高阶函数

HOC: higher order component高阶组件
一个组件可以接收另一个组件作为参数,这种组件就称之为高阶组件。
高阶组件(HOC)是 React 中用于复用组件逻辑的一种高级技巧。
具体而言,高阶组件是参数为组件,返回值为新组件的函数。

组件画分:UI 组件,ui组件。容器组件,处理逻辑

例子:登录和注册组件复用太高。

//公共组件   容器组件
import React, {Component} from 'react';

export default (WrapperComponent)=>{
    return class Hoc extends Component{
        constructor() {
            super();
            this.state={
                user:'',
                pwd:'',
                rePwd:''//确认密码
            }
        }
        //onChange事件
        onChange =(type,value)=>{
            this.setState({
                [type]:value
            })
        }
        handleChange = (e,type)=>{
            this.onChange(type,e.target.value);
         }
        //处理提交
        handleSubmit=(e)=>{
            let {user,pwd,rePwd} = this.state;
            //如果存在确认密码
            rePwd? console.log("用户名:",this.state.user,"密码:",this.state.pwd,"确认密码:",this.state.rePwd)
             :console.log("用户名:",this.state.user,"密码:",this.state.pwd);
         }
         render() {
            let Method= {
                handleChange:this.handleChange,
                handleSubmit:this.handleSubmit
            }
            return (<div>
                <WrapperComponent {...this.state} {...Method}/>
            </div>)

         }
    }
}
//登录页面      UI组件
import withHoc from './Hoc'
import React from "react";

function Login(props) {
    let {user,pwd,handleChange,handleSubmit} = props;
   return (

       <div>
          <h2>登录UI</h2>
           <p>用户名 <input type="text" value={user} placeholder='用户名'
                         onChange={(e)=>handleChange(e,'user')}/></p>
           <p>密码 <input type="text" value = {pwd} placeholder='密码'
                        onChange={(e)=>handleChange(e,'pwd')}/></p>
           <p><button onClick={ (e)=>handleSubmit()}>登录</button></p>
       </div>
   )
}

export default withHoc(Login)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值