npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@efox/reacttrace

v1.1.3

Published

A package tool to handle trace in React

Downloads

25

Readme

@efox/reacttrace

给react应用注入埋点代码,实现业务代码和埋点代码隔离

初始化(ReactTrace)

import http from "./http";
import { useStores } from "./stores"; // 具体可看example项目
import ReactTrace, {Trace} from '@efox/reacttrace'

const components = {}

// 1. 实例化trace
const trace = new ReactTrace({
  components,
  axios,
  useStores
})
// 2. 暴露useTraceHook钩子
const useTraceHook = trace.useTraceHook

// 3. 给APP组件包裹上Trace组件
<Trace>
  <App />
</Trace>

ReactTrace初始化参数:Object

| Name | Type | Description | | --------- | ----------------------- | --------------------------------------------------- | | components | object | 以组件作为划分单位的埋点逻辑,格式如{[组件名]: [{单个处理行为,格式如ComponentItem格式}]},详细使用可见以下篇章| | axios | Axios | Axios实例 | | useStores | Function | mobx的store方法,具体看example使用 |

组件中注入hook(useTraceHook)

假如我们有个组件函数,名字叫about,则在该组件引入hook书写如下:


const [count, setCount] = useState(1)

useTraceHook('about', { // 埋点逻辑中需要用到的局部变量
    count
})

useTraceHook参数:

参数1: string, 组件名,作为唯一id

参数2: object, 需要监听的使用useState生成的局部变量集合

components使用

ComponentItem参数:Object

| Name | Type | Description | | --------- | ------ | --------------------------- | | trigger | string | 处理类型,如事件和模拟生命周期 | | el | string | 事件触发时,需要填写元素目标 | | listener | ({$store, $scope, watch, request}) => void | 在对应时机触发的回调函数,具体使用看listener方法使用 |

about组件注入hook后,需要在components中写about的key值对应的埋点逻辑,具体场景划分为两类:

1. 用户行为事件触发

比如针对id为clickbutton的dom元素的用户Click事件的监听后,需要执行对应的埋点逻辑:

const components = {
    about: [
        {
            trigger: 'click', // 针对某个元素的用户行为监听
	        el: '#clickbutton',
	        listener: ({$scope, $store, request, watch}) => {
                // 埋点逻辑代码
	    	}
        }
    }
    ]
}

2. react生命周期钩子触发

比如

const components = {
    about: [
        {
            trigger: 'componentDidMount', // 在componentDidMount生命周期钩子运行
            listener: ({watch, $scope, $store, request, event}) => {
                // 埋点逻辑代码
            }
        }
    ]
}

listener方法使用

函数参数说明: object

| Name | Type | Description | | --------- | ------ | --------------------------- | | $store | object | 全部变量,useStores()生成 | | $scope | object | 组件局部变量,需要手动传入 | | watch | Function | 监听$store或者$scope方法 | | request | Function | 拦截http请求处理 |

watch方法

watch参数:Object

| Name | Type | Description | | --------- | ------ | --------------------------- | | 参数1 | string/string[] | 监听$store或者$scope下的变量,可以混合监听 | | 参数2 | (cur, pre) => void | 监听变动处理函数, cur:现在的值 pre:上一个值 |


// 监听$store基础变量
watch('$store.aboutStore.name', async (cur, pre) => {
  // 埋点逻辑
})
// 监听$store引用变量
watch('$store.aboutStore.person', async (cur, pre) => {
  // 埋点逻辑
})
// 同时监听多个$store值
watch(['$store.aboutStore.name', '$store.aboutStore.person'], async (cur, pre) => {
  // 埋点逻辑
}
// 监听$scope基础变量
watch('$scope.count', async (cur, pre) => {
  // 埋点逻辑
})
// 监听$scope引用变量
watch('$scope.query', async (cur, pre) => {
  // 埋点逻辑
})
// 监听$scope引用变量中的某个具体值
watch('$scope.query.page', async (cur, pre) => {
  // 埋点逻辑
})
// 同时监听多个$scope值
watch(['$scope.count', '$scope.query'], async (cur, pre) => {
  // 埋点逻辑
}
// 混合监听$store和$scope变量
watch(['$store.aboutStore.name', '$scope.query'], async (cur, pre) => {
  // 埋点逻辑
})

request方法