启动
npm run dev:weapp
taro build --type weapp --watch
样式相关
- 使用
flex布局,主轴方向默认column - 使用
className/style添加样式 app.js引入的就是全局样式background等价于background-colorbackground-image特性使用<Image />标签配合flex布局实现- 组件样式隔离
- 外部样式类
externalClasses - 全局样式类
options.addGlobalClass
- 外部样式类
命名规范
- 普通文件命名
util.jsutil_helper.js - 组件文件命名
ReservationCard.jsx - 变量名
const myVar = 'hello' - 类名要以大写字母开头
class Animal {} - 属性名
<Foo userName='hello' phoneNumber={12345678}/>
书写顺序
static静态方法constructorcomponentWillMountcomponentDidMountcomponentWillReceivePropsshouldComponentUpdatecomponentWillUpdatecomponentDidUpdatecomponentWillUnmount- 点击回调或者事件回调 比如
onClickSubmit()或者onChangeDescription() render
JSX 书写规范
- 不要以
class/id/style作为自定义组件的属性名 - 不要使用
HTML标签 - 不要使用
undefined来初始化变量 map循环时请给元素加上key属性- 尽量避免在
componentDidMount中调用this.setState - 不要在
componentWillUpdate/componentDidUpdate/render中调用this.setState - 组件最好定义
defaultProps - 值为
true的属性可以省略书写值 - 事件绑定均以
on开头 - 子组件传入函数时属性名需要以
on开头 - 必须声明
Taro
生命周期
| 生命周期 | 入口文件 | 页面 | 组件 |
|---|---|---|---|
| componentWillMount() | X | X | X |
| componentDidMount() | X | X | X |
| componentDidShow() | X | X | |
| componentDidHide() | X | X | |
| componentWillReceiveProps(nextProps) | X | ||
| shouldComponentUpdate(nextProps, nextState) | X | X | |
| componentWillUpdate(nextProps, nextState) | X | X | |
| componentDidUpdate(prevProps, prevState) | X | X | |
| componentWillUnmount() | X | X | |
| componentDidCatchError(String error) | X | ||
| componentDidCatchError(Object) | X |
页面事件处理函数
- onPullDownRefresh()
- onReachBottom()
- onPageScroll(Object)
- onShareAppMessage(Object)
- onResize(object)
- onTabItemTap(Object)
- componentWillPreload()
配置
路由
Taro.navigateTo({ url: '/pages/page/path/name' })
Taro.redirectTo({ url: '/pages/page/path/name' })
路由传参
方式一:
- 添加参数
Taro.navigateTo({ url: '/pages/page/path/name?id=2&type=test' })
- 获取参数
this.$router.params
方式二(小程序中):
- 添加参数
this.$preload('key', 'val')
Taro.navigateTo({ url: '/pages/B/B' })
- 获取参数
this.$router.preload
语法特性
条件渲染
- 使用变量来储存元素
let st = <Text>XXX</Text> - 逻辑运算符
&& - 条件表达式
?: - 枚举条件
{..}[status]
列表渲染
map+key
插槽
- 默认插槽
this.props.children - 具名插槽
this.props.renderXyyy(组合都必须用render开头) - 作用域插槽
this.props.renderXyz(this.state)
函数式组件
- 普通函数式组件(纯函数)
- 类函数式组件(在在类组件内部)
- 闭包函数式组件(在普通函数式组件内部)
引用 Ref
xxRef = (node) => this.xx = node<Comp ref={this.xxRef} />this.xx = Taro.createRef()<Comp ref={this.xx} />const xx = useRef(null)<Comp ref={xx} />
数据传输
- props 父子间通信
- Context 发布订阅模式
- Taro.createContext
- Context.Provider
- Class.contextType / useContext
- events 消息机制
- Taro.eventCenter.on
- Taro.eventCenter.trigger
- Taro.eventCenter.off
- storage 数据缓存
- Taro.setStorage
- Taro.getStorage
- Taro.removeStorage
- Taro.clearStorage
Hooks
useState
用
useReducer替代
const [state, setState] = useState(initialState)
const [state, setState] = useState(() => {
const initialState = someExpensiveComputation(props)
return initialState
})
useEffect
useEffect(() => {
let num = 0
let timeId = setInterval(() => {
console.log(num++)
}, 1e3)
return () => {
clearInterval(timeId)
}
}, [])
useReducer
function init(initialCount) {
return { count: initialCount }
}
function reducer(state, action) {
switch (action.type) {
case 'increment':
return { count: state.count + 1 }
case 'decrement':
return { count: state.count - 1 }
case 'reset':
return init(action.payload)
default:
throw new Error()
}
}
export default function Counter({ initialCount }) {
const [state, dispatch] = useReducer(reducer, initialCount, init)
return (
<View>
Count: {state.count}
<Button
onClick={() =>
dispatch({ type: 'reset', payload: initialCount })
}
>
Reset
</Button>
<Button onClick={() => dispatch({ type: 'increment' })}>+1</Button>
<Button onClick={() => dispatch({ type: 'decrement' })}>-1</Button>
</View>
)
}
useCallback
const memoizedCallback = useCallback(() => {
doSomething(a, b)
}, [a, b])
useContext
const value = useContext(MyContext)
useMemo
const memoizedValue = useMemo(() => computeExpensiveValue(a, b), [a, b])
useRef
const refContainer = useRef(initialValue)
其他
- 使用
this.$componentType来判断当前Taro.Component是页面还是组件- 取值
PAGE和COMPONENT
- 取值
- 内置环境变量
process.env.TARO_ENV- 取值
weapp/h5/swan/alipay/rn/tt/qq/quickapp
- 取值
- 多端组件文件
test.js/test.weapp.js/test.h5.js/test.swan.js/test.qq.js/test.quickapp.js- 多端文件对外的接口要保持一致
- 环境判断
Taro.ENV_TYPE/Taro.getEnv()ENV_TYPE.WEAPP- 微信小程序环境ENV_TYPE.SWAN- 百度小程序环境ENV_TYPE.ALIPAY- 支付宝小程序环境ENV_TYPE.TT- 字节跳动小程序环境ENV_TYPE.WEB- WEB(H5)环境ENV_TYPE.RN- ReactNative 环境
- 消息机制
Taro.Events/Taro.eventCenterevents.on('eventName', handler)events.trigger('eventName', ...arg)events.off('eventName', [handler])events.once('eventName', handler)events.off()