2.3.7 • Published 3 years ago

gentleman-sample-sdk v2.3.7

Weekly downloads
-
License
MIT
Repository
github
Last release
3 years ago

sample-sdk

Install

npm i gentleman-sample-sdk

Usage (Nextjs)

1. create context

import React, { useContext, createContext, useState } from 'react'
import { GentleInstance } from 'gentleman-sample-sdk'

const GentleSDKContext = createContext<GentleInstance | null>(null)

interface IProps {
  gentleClient: GentleInstance
}

export const GentleProvider: React.FC<IProps> = ({ gentleClient, children }) => {
  const [state] = useState<GentleInstance>(gentleClient)

  return <GentleSDKContext.Provider value={state}>{children}</GentleSDKContext.Provider>
}

export const useGentle = () => {
  const contextValue = useContext(GentleSDKContext)
  if (contextValue !== null) return contextValue

  throw new Error('ContextValue is null!')
}

2. in _app.tsx

import { GentleProvider } from '../context/gentleContext'
import { createGentleInstance } from 'gentleman-sample-sdk'

function MyApp({ Component, pageProps }: AppProps) {
  let gentleClient
  // baseUrl of log server
  if (process.browser) gentleClient = createGentleInstance({ baseUrl: 'http://localhost:5000' })

  if (gentleClient) {
    return (
      <GentleProvider gentleClient={gentleClient}>
        <Component {...pageProps} />
      </GentleProvider>
    )
  }

  return <Component {...pageProps} />
}
export default MyApp

3. useGentle

import { useGentle } from '../context/gentleContext'

interface IProps extends InferGetServerSidePropsType<typeof getServerSideProps> {}

const Home: React.FC<IProps> = ({ user }) => {
  const gentleClient = useGentle()

  useEffect(() => {
    // post event data to 'http://localhost:5000/logs'
    gentleClient.track<T>({
      endPoint: '/logs',
      event: { eventName: 'view', eventProperties: { page: 'home' } },
    })
  }, [])

  return (
    <div>
      <h1>HOME</h1>
    </div>
  )
}
2.3.7

3 years ago

2.3.6

3 years ago

2.3.0

3 years ago

2.3.2

3 years ago

2.3.1

3 years ago

2.3.4

3 years ago

2.3.3

3 years ago

2.3.5

3 years ago

2.2.1

3 years ago

2.2.0

3 years ago

2.1.8

3 years ago

2.1.7

3 years ago

2.1.9

3 years ago

2.1.10

3 years ago

2.1.11

3 years ago

2.1.6

3 years ago

2.1.5

3 years ago

2.1.2

3 years ago

2.1.1

3 years ago

2.1.4

3 years ago

2.0.5

3 years ago

2.1.3

3 years ago

2.0.7

3 years ago

2.0.6

3 years ago

2.0.9

3 years ago

2.0.8

3 years ago

2.1.0

3 years ago

2.0.3

3 years ago

2.0.4

3 years ago

1.1.6

3 years ago

2.0.2

3 years ago

2.0.1

3 years ago

2.0.0

3 years ago

1.1.1

3 years ago

1.0.2

3 years ago

1.1.0

3 years ago

1.0.1

3 years ago

1.0.9

3 years ago

1.0.8

3 years ago

1.0.7

3 years ago

1.1.5

3 years ago

1.0.6

3 years ago

1.1.4

3 years ago

1.0.5

3 years ago

1.1.3

3 years ago

1.0.4

3 years ago

1.1.2

3 years ago

1.0.3

3 years ago

1.0.0

3 years ago