react从0搭建项目

2020-12-04  本文已影响0人  青争小台

一、引入iconfont阿里巴巴图库

官网https://www.iconfont.cn/home/index?spm=a313x.7781069.1998910419.2
1.进入官网
2.把需要的图标加入购物车
3.把购物车里的图标加入项目
4.点击下载至本地,解压
5.在项目的public文件夹下新建一个iconfont文件夹
6.把如图所示的几个文件,放入iconfont文件夹内
7.在public/index.html里引入/iconfont/iconfont.css<link rel="stylesheet" href="./iconfont/iconfont.css">
8.在组件内使用<i className="iconfont">&#xe679;</i>

image.png
image.png

二、路由传参

<Route path='/path/:name' component={Path}/>
<link to="/path/2">xxx</Link>
this.props.history.push({pathname:"/path/" + name});
读取参数用:this.props.match.params.name
<Route path='/query' component={Query}/>
<Link to={{ path : ' /query' , query : { name : 'sunny' }}}>
this.props.history.push({pathname:"/query",query: { name : 'sunny' }});
读取参数用: this.props.location.query.name
<Route path='/sort ' component={Sort}/>
<Link to={{ path : ' /sort ' , state : { name : 'sunny' }}}> 
this.props.history.push({pathname:"/sort ",state : { name : 'sunny' }});
读取参数用: this.props.location.query.state 
<Route path='/web/departManange ' component={DepartManange}/>
<link to="web/departManange?tenantId=12121212">xxx</Link>
this.props.history.push({pathname:"/web/departManange?tenantId" + row.tenantId});
读取参数用: this.props.location.search

三、ref的3种方法

image

注意: react并不推荐过度使用ref,如果能通过state做到的事情,就不应该使用 refs 在你的 app 中“让事情发生”。
过度使用ref并不符合数据驱动的思想

摘自:https://www.cnblogs.com/lanpang9661/p/12604712.html

四、将npm run build后,点击build文件夹内的index.html文件直接查看页面

1.在package.json里,"private":true后加"homepage": "./"

image.png
2.如果用的是BrowserRouter:history路由则还会报错,这时候要把BrowserRouter换成HashRouter:哈希路由
image.png

五、react实现vue插槽

子组件

import React, { Component } from 'react';

class Zi extends Component {
    constructor(props) {
        super(props);
        this.state = {}
    }
    render () {
        const { children } = this.props
        return <div>
            {Array.isArray(children) ?
                children.map(child => child) :
                children && children
            }
            <p>渲染子组件自带的元素</p>
        </div>
    }
}

父组件

import React, { Component } from 'react';
import Zi from './zi.js'

class Fu extends Component {
    constructor(props) {
        super(props);
        this.state = {}
    }
    render () {
        return <div>
            <Zi>
                <p>渲染父的元素1</p>
                <p>渲染父的元素2</p>
            </Zi>
        </div>
    }
}
上一篇 下一篇

猜你喜欢

热点阅读