1.1.6 • Published 9 months ago

@jamsocket/react v1.1.6

Weekly downloads
-
License
MIT
Repository
github
Last release
9 months ago

@jamsocket/react

GitHub Repo stars Chat on Discord npm

React hooks for interacting with session backends and the Jamsocket platform.

Read the docs here

Installation

npm install @jamsocket/react

Example

Here's an example of how different parts of Jamsocket's client libraries work together.

import { Jamsocket } from '@jamsocket/server'

const jamsocket = new Jamsocket({
   account: '[YOUR ACCOUNT]',
   token: '[YOUR TOKEN]',
   service: '[YOUR SERVICE]',
   // during development, you can simply pass { dev: true }
})

const connectResponse = await jamsocket.connect() // returns an instance of ConnectResponse
import { type ConnectResponse, SessionBackendProvider, useReady } from '@jamsocket/react'

function Root() {
  return(
    <SessionBackendProvider connectResponse={connectResponse}>
      <MyComponent sessionBackendUrl={connectResponse.url} />
    </SessionBackendProvider>
  )
}

function MyComponent({ sessionBackendUrl }) {
  const ready = useReady()

  useEffect(() => {
    if (ready) {
      // make a request to your session backend
      fetch(sessionBackendUrl)
    }
  }, [ready])

  return ready ? <MyChildren /> : <Spinner />
}

Library Reference

@jamsocket/react

SessionBackendProvider

Wrap the root of your project with the SessionBackendProvider so that the children components can utilize the React hooks.

The SessionBackendProvider must be used in conjunction with @jamsocket/server in order to access the connect response returned by the connect function.

import { SessionBackendProvider, type ConnectResponse } from '@jamsocket/react'

export default function HomeContainer({ connectResponse }: { connectResponse: ConnectResponse }) {
  return (
    <SessionBackendProvider connectResponse={connectResponse}>
        <Home />
    </SessionBackendProvider>
  )
}

useReady

Is a React hook that returns a boolean that is true if the session backend is ready.

import { useReady } from '@jamsocket/react'

const isReady = useReady()

Other exports

The @jamsocket/react package also re-exports all of the @jamsocket/client and @jamsocket/types packages' exports, including classes and types.

1.1.6

9 months ago

1.1.5

9 months ago

1.1.4

11 months ago

1.1.3

11 months ago

1.1.2

1 year ago

1.1.1

1 year ago

1.1.0

1 year ago

1.0.0

1 year ago

0.2.5

1 year ago

0.2.4

1 year ago

0.2.3

2 years ago

0.2.2

2 years ago

0.2.1

2 years ago

0.2.0

2 years ago