Moment For Technology

Click Taro+TypeScript to return to the top of any page

Posted on May 26, 2023, 11:49 a.m. by Michael Mcclain
Category: The front end Tag: The front end taro

Store a variable locally. Check to see if it exists when you arrive at the home page. Check to see if it exists when you return to the home page again. Click on any page to return to the home page:

View className='item' onClick={()=  goToCard()}
    Image className='mingpian_icon' src={mingpian} /
    Text className='contact'Business card/Text
Copy the code
import { storage } from '@utils';
const goToCard = () =  {
Copy the code

Back to the top operation of the home page :(remember to clear after returning to the top)

import { storage } from '@utils';
 useDidShow(() =  {
    const isTop = storage.getLocalStorage('isTop');
    if (isTop) {
      storage.removeLocalStorage('isTop'); }});Copy the code
//storage.ts encapsulates Taro's original local API
import Taro from '@tarojs/taro';
/ / get LocalStorage
const getLocalStorage = (name) =  {
  try {
    const value = Taro.getStorageSync(name);
    return value;
  } catch (e) {
    // error}};/ / set the LocalStorage
const setLocalStorage = (name, value) =  {
  try {
    Taro.setStorageSync(name, value);
  } catch (e) {
    // error}};/ / remove LocalStorage
const removeLocalStorage = (name) =  {
  try {
  } catch (e) {
    // error}};// Clear the local cache
const clearLocalStorage = () =  {

export default {

Copy the code
The index in the / / utils. Ts
export { default as storage } from './storage';
Copy the code
About (Moment For Technology) is a global community with thousands techies from across the global hang out!Passionate technologists, be it gadget freaks, tech enthusiasts, coders, technopreneurs, or CIOs, you would find them all here.