React 基础3

本文详细介绍了React及其生态系统的使用方法,包括数据获取、生命周期管理、路由设置、Material UI组件应用及React Native脚手架搭建,是React全栈开发者不可或缺的资源。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

React 获取服务器的数据 ajax

  • axios cros 跨域
    安装模块 npm install axios --save/cnpm install axios --save
    引入模块 import axios from ‘axios’
import React,{Component} from 'react';
import Axios from 'axios';//引入请求服务器的模块
componentDidMount(){
 		let api="http://www.phonegap100.com/appapi.php?a=getPortalList&catid=20";
		//里面是用this  注意this的指向
  		Axios.get(api).then( (res)=>{
            console.log(res);
        }).catch( (err)=>{
            console.log(err);
        }).finally(()=>{//最终执行操作
		});
    }
  • fetch jsonp 跨域
    安装模块 npm install fetch-jsonp --save/cnpm install fetch-jsonp --save
    引入模块 import Fetchjsonp from ‘fetch-jsonp ’
    componentDidMount(){
        let api="http://www.phonegap100.com/appapi.php?a=getPortalList&catid=20";
        fetchjsonp(api).then( (res)=>{ //这里对数据进行操作   默认的           
            return res.json();
        }).then( (res)=>{
            console.log(res);
        }).catch( (err)=>{
            console.log(err);
        });
    }

React 生命周期函数

组件挂载 >>> 组件数据更新 >>> 组件销毁

    componentWillMount(){
        console.log("组件挂载之前");
    }
    componentDidMount(){
        console.log("组件挂载完成");
    }
    //是否更新数据的生命周期函数  返回Boolean  返回true更新
    shouldComponentUpdate(){        
        return true;
    }
    //将要更新数据的函数
    componentWillUpdate(){
        console.log("数据更新之前");
    }
    //数据更新完成
    componentDidUpdate(){
        console.log("数据更新完成");
    }
    //组件销毁的时候执行的生命周期函数
    componentWillUnmount(){
        console.log("组件卸载");
    }

React 路由

安装路由 cnpm install react-router-dom –save
安装url模块 cnpm install url –save 解析get传值路由url.parse(this.props.location.search,true).query

// 1.要在项目里面使用路由先导入路由
import {BrowserRouter as Router, Route,Link} from 'react-router-dom';

//2.导入路由子组件
import Home from './component/Home'
import News from './component/News'
import Product from './component/Product'
import Child1 from './component/Product/child1'
//3.3编程式导航
back(){  //路由回退
	   this.props.history.go(-1));
}
gohome(){//路由回首页
        console.log(this.props);
        //编程式导航
        this.props.history.push("/");
        this.props.history.push("/",10); //传值到this.props.location.state
        this.props.history.replace("/",20);//传值
}
//3.动态路由传值  get传值
render(){
	let match=this.props.match.path;//当前路由
	return(	
		<div>
			<Router>
			//3.1路由入口
				<link to="/">Home</link>	
//动态路由传值到 this.props.match.params.id		
<link to="/News/:id">News </link>
//get传值  相当于在路径上加?id=1 导入node的url模块进行解析
//get传值到 url.parse(this.props.location.search,true).query
<Link to={`/News?id=${value.id}`}>{value.title}</Link>
				<link to="/Product">Product</link>
				<link to={`${match}/child1`}>Child1</link> //let match=this.props.match.path;
				//3.2路由Route
				<Switch>
					<Route exact path="/" compoent={Home}></Route> //严格模式
					<Route path="/News" compoent={News}></Route>
					<Route path="/Product" compoent={Product}></Route>
					<Route path={`${match}/child1`} compoent={Child1}></Route>
					//重定向 默认child1
					<Redirect path={`${match}`} to={`${match}/child1`}></Redirect>
				</Switch>
			</Router>
			//3.3编程式导航 回退 首页
			<button onClick={this.back.bind(this)}>路由回退</button>
			<button onClick={this.gohome.bind(this)}>回到首页</button>
		</div>)}

React Material UI 组件

npm安装npm install @material-ui/core
yarn安装yarn add @material-ui/core

React native 脚手架

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值