Skip to main content

6 posts tagged with "react"

View All Tags

· One min read
npm i react-hls-player --save
import React from 'react';
import ReactHlsPlayer from "react-hls-player";
//这个库有缺陷 好久没维护了 只支持到react16
//另外video必须是.m3u8格式的

export default class HlsPlayer extends React.Component {
render(){
const { url } = this.props
return (
<ReactHlsPlayer
src={url}
autoPlay={false}
controls={true}
width="100%"
height="auto"
/>
)
}
}
<HlsPlayer
src={"https://video.scott-xiong.com/movie/2/output.m3u8"}
/>

· 2 min read

<Empty/>

import { Empty } from 'antd';

const App = () => <Empty image={Empty.PRESENTED_IMAGE_SIMPLE} />;
export default App;

<Button>

import { Button, Space } from 'antd';


<Button type="primary" size='small' danger>
Button
</Button>

<Icon/>

import { AreaChartOutlined } from '@ant-design/icons';

<AreaChartOutlined />

<Tag/>

import { Tag } from 'antd';

const App = () => <Tag color="magenta">magenta</Tag>;
export default App;

<Pagination/>

import { Pagination } from 'antd';

const App = () => <Pagination
defaultCurrent={1}
pageSize={page_size}
total={all_items}
current={current_page}
onChange={this.pageChange}
/>;
export default App;

API

<Table/>

一般Table组件搭配Empty组件使用

antd的table组件是真tm难用啊,很多人反应数据达到200+,卡的一批,知乎:为什么Ant-Design的Table性能这么低?

但是table还是得用啊,临时解决方案用的是react-bootstrap

# 1. install
npm install react-bootstrap --save
# 2, public index.html引入样式文件
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/latest/css/bootstrap.min.css">

使用:

import React, { Component } from 'react'
import { Table} from 'react-bootstrap';

export default class Test extends Component {
render() {
return (
<div>
<Table striped bordered condensed='true' hover>
<thead>
<tr>
<th>#</th>
<th>First Name</th>
<th>Last Name</th>
<th>Username</th>
</tr>
</thead>
<tbody>
<tr>
<td>1</td>
<td>Mark</td>
<td>Otto</td>
<td>@mdo</td>
</tr>
<tr>
<td>2</td>
<td>Jacob</td>
<td>Thornton</td>
<td>@fat</td>
</tr>
<tr>
<td>3</td>
<td colSpan="2">Larry the Bird</td>
<td>@twitter</td>
</tr>
</tbody>
</Table>

</div>
)
}
}

效果如下:

<Input/>

import { Input } from 'antd';
const App = () => <Input placeholder="Basic usage" />;
export default App;

import { Input } from 'antd';
const { Search } = Input;

<Search
placeholder="input search text"
allowClear
enterButton="Search"
size="large"
onSearch={onSearch}
/>

API

· 3 min read

create-react-app用get向后端发送请求的时候是OK的,但是换成post请求就会报跨域请求的错误--CORS

参考官网有2种解决方法:

这里演示如何将 http://127.0.0.1:3001/test开始的api代理到http://127.0.0.1:8002

方案1:修改package.json:

添加一行配置,但是react新版只支持字符串的配置,这就意味着如果你无法请求多个域名的api

"proxy": "http://localhost:8002",

注意:此时,前端请求的时候就不用加BASE_URL

方案2:创建src/setupProxy.js,注意必须是这个文件名

npm i http-proxy-middleware --save

创建src/setupProxy.js

const { createProxyMiddleware } = require('http-proxy-middleware');

module.exports = function(app) {
app.use(
'/test',
createProxyMiddleware({
target: 'http://127.0.0.1:8002',
changeOrigin: true,
})
);
};

此时再去发动post请求:

import React, { Component } from 'react'
import pageStyle from '../../page.module.css'
// import homeStyle from './index.module.css'
import axios from "axios";
export default class ExampleHome extends Component {
state = {
arr:[]
}
componentDidMount() {
axios.post("/test/1",{}).then(
res=>{
console.log(res.data.data)
this.setState({
arr : res.data.data
})
},
err=>{}
)
}

render() {
return (
<div className={pageStyle.container}>
{
this.state.arr.map((item,index)=>{
return <li key={index}>{item}</li>
})
}
</div>
)
}
}

发现没有报错了,页面可以正常显示

值得注意的是, npm run build之后,代理是失效的,需要借助nginx caddy等代理工具完成代理

有人说请求的时候把BASE_URL加上不就行了,我们来试试?

修改前端react代码:

import React, { Component } from 'react'
import pageStyle from '../../page.module.css'
// import homeStyle from './index.module.css'
import axios from "axios";
export default class ExampleHome extends Component {
state = {
arr:[]
}
componentDidMount() {
const BASE_URL = "http://127.0.0.1:8002"
axios.post(`${BASE_URL}/test/1`,{}).then(
res=>{
console.log(res.data.data)
this.setState({
arr : res.data.data
})
},
err=>{}
)
}

render() {
return (
<div className={pageStyle.container}>
{
this.state.arr.map((item,index)=>{
return <li key={index}>{item}</li>
})
}
</div>
)
}
}

打开浏览器控制台,发现已经CORS报错了

点开 network, 已经飙红了,发现请求压根都没到达后端就被浏览器拦截了:

详细信息:

我们来看看编译后的代码:

访问url: 控制台和network都报错

总结

react在开发阶段可以用src/setupProxy.js来实现代理,部署的时候需借助nginx caddy等代理软件配置代理

· 3 min read

react核心思想

通过数据操作dom,数据改变 dom会重新render;所以我们的侧重点是修改数据(修改数据要用到setState)

原则:immutable原则,不能直接修改state,要先copy一下

事件

事件名称首字母大写,eg: html 中 on-click => react: onClick

构造函数

构造函数是最先被执行的,所有this的绑定在这里执行可以节约性能

构造函数默认接受props,下面是固定写法

constructor(props) {
super(props)
}

jsx语法

凡事js出现html都要引入"react"

注释

{}放的是js,注释可以放在里面

style={{color:'red',fontSize:12px}} => 外层{}是放js的, 内层{}是一个js对象

渲染的时候不转义,虽然有可能被注入恶意代码,但是我们有这个需求:

<ul>
{
this.state.list.map((item,index)=>{
return <li key={index} dangerouslySetInnerHTML={__html: item}></li>
})
}
</ul>

可以看到所有html中套js {}最终返回的只是html/组件

属性冲突

  • label标签里的for => htmlFor
  • 普通标签的class => className

es6语法

return 可以简写成();只有一行的话,return是可以省略的

对象赋值:

const name = this.props.name;
const age = this.props.age;

//等效于
const {name, age} = this.props;

复制/拷贝:

var list = ["apple","pear","orange"]
var another = [...list] //把list中的元素一一展开,再用[]包裹成array

对象属性中,如果key字符串和value对应的变量名同名,可以简写,如下:

const list = [...this.state.list,"apple"]

this.setState({
list,
inputValue: ""
})

state

只要是react组件即class 无论是是在jsx语法中还是在其他函数中 要访问state属性 必须是 this.state.xxx

组件间传值

传值:父组件可以通过添加属性的方式向子组件中传递任何属性和方法(方法需要绑定父组件this,bind还可以接受参数,一般为索引index)

接收: 子组件通过this.props.xxx接收