跳至主要内容

博文

目前显示的是 五月, 2023的博文

Taro 中通过动态插入节点实现 API Toast

 原文链接 什么是动态插入节点 在 web 端加入我们需要实现一个可以通过API调用的  Toast ,我们可以这样实现: const Toast = ( props ) => { return < div > { props . msg } < / div > } const showToast = ( options ) => { const div = document . createElement ( 'div' ) ; div . setAttribute ( 'id' , 'toast' ) ; document . body . appendChild ( div ) ; ReactDOM . render ( React . createElement ( Toast , options ) , div ) ; } const hideTosat = ( ) => { if ( ! document . getElementById ( "toast" ) ) return ; document . getElementById ( "toast" ) . remove ( ) } Copy 我们依赖的就是浏览器 Document 对象,提供的可以操作 dom 的方法。但是我们都知道小程序没有提供 Document 对象,那么我们该如何去实现类似的功能呢? 在 taro 中动态插入节点的实现 import React from 'react' ; import Taro from '@tarojs/taro' ; import { render , unmountComponentAtNode } from '@tarojs/react' ; import { document , TaroRootElement } from '@tarojs/runtime' ; import { View } ...