赞
踩
npx create-react-app my-app
const React = {
createElement,
Component
}
React.createElement: 创建虚拟DOM
React.Component: 实现自定义组件
主要是render逻辑。
ReactDOM.render: 渲染真实DOM
使用:函数组件通过props接收数据,类组件通过this.props接收数据。
特点:传递任意类型的数据;props是只读的对象;
Mouse.propTypes = {
children: PropTypes.func.isRequired
}
Mouse.defaultProps = {
children: ""
}
class Parent extends React.Component{ getMsgCallback = data=>{ console.log(data) } render(){ return ( <div> <Child getMsgCallback={this.getMsgCallback}> </div> ) } } class Child extends React.Component{ handleClick= ()=>{ this.props.getMsgCallback("子传父") } render(){ return ( <div> <button onClick={this.handleClick}/> </div> ) } }
跨组件传递数据
const {Provider,Consumer}=React.createContext()
<Provider value="">
</Provider>
<Consumer>
{data => {}}
</Consumer>
常用钩子函数:
旧版钩子函数:
新版钩子函数:
实现状态逻辑复用,状态指的是state,逻辑指的是setState。
class Mouse extends React.Component{
render(){
return this.props.render(this.state)
}
}
<Mouse render={(mouse)=><p>鼠标当前位置{mouse.x},{mouse.y}</p>}/>
children代替render属性
class Mouse extends React.Component{
render(){
return this.props.children(this.state)
}
}
<Mouse>
{(mouse)=><p>鼠标当前位置{mouse.x},{mouse.y}</p>}
</Mouse>
添加props校验
import PropTypes from 'prop-types'
Mouse.propTypes = {
children: PropTypes.func.isRequired
}
高阶组件之于组件相当于手机壳之于手机。
const EnhancedComponent = withHOC(WrappedComponent)
function withMouse(WrappedComponent){
class Mouse extends React.Component{
return <WrappedComponent {...this.state}/>
}
return Mouse
}
const MousePosition = withMouse(Position)
<MousePosition/>
得到两个组件名称相同
设置displayName能在调试时区分不同组件
Mouse.displayName = `WithMouse${getDisplayName(WrappedComponent)}`
function getDisplayName(WrappedComponent){
return WrappedComponent.displayName || WrappedComponent.name || 'Component'
}
props丢失
<WrappedComponent {...this.state} {...this.props}/>
作用:修改state;更新组件
注意:
异步更新数据;
多次调用setState函数只触发一次重新渲染;
setState(updater[,callback]),第二个参数在状态更新且页面完成渲染后立即执行某个操作;
// 经典写法
this.setState({
count: this.state.count+1
})
// 推荐写法,多次调用setState函数的顺序更符合人类的思维顺序
this.setState((state,props)=>{
return {
count: state.count+1
}
})
JSX是createElement函数的简化语法。
JSX语法被@babel/preset-react插件编译为creatElement函数。
React元素:描述看到的内容。
JSX和JSX预处理后的代码:
只存储与渲染相关的数据
使用钩子函数shouldComponentUpdate(nextProps, nextState)
自动实现了shouldComponentUpdate钩子函数;
使用浅层对比,对于引用类型,只比较对象的地址;
const newObj = {...state.obj, number: 2}
setState({obj: newObj})
this.setState({
list: [...this.state.list, {新数据}]
})
实现:虚拟DOM配合Diff算法
虚拟DOM:React元素,本质是JS对象,描述UI。用JS对象表示DOM信息和结构,当状态变更的时候,重新渲染这个JS的对象结构
const element={
type: 'h1',
props: {
className: 'greeting',
children: 'Hello JSX'
}
}
Diff过程:
<Route exact path="/" component=... />
在线找房、用户登陆、房源发布
React核心库:react、react-dom、react-router-dom
脚手架:create-react-app
数据请求:axios
UI组件库:antd-mobile
其他组件库:react-virtualized、formik+yup、react-spring
百度地图API
注意node-sass依赖的问题,多次yarn操作后可以成功安装,但默认情况下node-sass的版本与node的版本不一致。(着实纳闷)
condition:
node -v v14.17.3
node-gyp -v v3.8.0
target:
yarn add node-sass@4.12.0
problem:
gyp verb `which` failed Error: not found: python2
solution:
yarn add global production windows-build-tools
navigator.geolocation.getCurrentPosition(position => {
console.log('当前位置信息:', position)
})
使用步骤:
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。