Skip to content

Commit

Permalink
Merge pull request #4 from Badiapp/tracking-consumer
Browse files Browse the repository at this point in the history
[feat] tracking consumer for class components
  • Loading branch information
jepser authored Apr 4, 2020
2 parents 2f329f4 + 4a85c2a commit c4bd2f1
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 3 deletions.
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,21 @@ const Room = ({ roomId }) => {
}
```
### Using the library with class components
For the cases that you need to use the library with class components, import the context and use it as a common [provider/consumer](https://reactjs.org/docs/context.html#reactcreatecontext) component:
``` jsx
import { TrackingContext } from 'treacker'

class MyClass extends React.Component {
componentDidMount() {
const tracking = this.context;

tracking.track('user-component.loaded')
}
}
MyClass.contextType = TrackingContext;
```
### HOC
WIP: `withTracking()`
Expand Down
2 changes: 1 addition & 1 deletion example/child-components.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React, { useEffect, useState } from 'react'

import { useTracking } from '../dist/bundle-esm'
import { useTracking } from '../dist/bundle.esm'

const ChildComponent = () => {
const tracking = useTracking()
Expand Down
2 changes: 1 addition & 1 deletion example/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React, { useState } from 'react'
import ReactDOM from 'react-dom'

import { TrackingProvider } from '../dist/bundle-esm'
import { TrackingProvider } from '../dist/bundle.esm'
import ChildComponent from './child-components'

function App () {
Expand Down
3 changes: 2 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export { trackingManager } from './lib/tracking'
export { registerTrackingListener } from './lib/helpers'
export { useTracking } from './react/hooks'
export { TrackingProvider } from './react/provider-consumer'
export { TrackingProvider, TrackingConsumer } from './react/provider-consumer'
export { withTracking } from './react/with-tracking'
export { TrackingContext } from './react/context'
2 changes: 2 additions & 0 deletions src/react/provider-consumer.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import PropTypes from 'prop-types'
import { trackingManager } from '../lib/tracking'
import { TrackingContext } from './context'

export const TrackingConsumer = TrackingContext.Consumer

export const TrackingProvider = ({
id,
children,
Expand Down

0 comments on commit c4bd2f1

Please sign in to comment.