Skip to content
This repository was archived by the owner on Aug 9, 2024. It is now read-only.
This repository was archived by the owner on Aug 9, 2024. It is now read-only.

RFC: useItem hook #20

@Kelin2025

Description

@Kelin2025

Case

Currrently there's useItemState and useItemApi hooks

But I came up with an idea of a more unified and convenient hook

Solution

  1. Unify shapes returned from APIs so we can use all the available APIs in a single hook RFC: Unify all "APIs" and namings #18
  2. Add useItem(api, key) hook that accepts all the entities - ListApi, ItemApi, MappingApi, SelectionApi
  3. Add <ItemProvider api={api} /> and support useItem(api) to solve prop-drilling and get rid of IDs
import { createListApi, createItemApi } from '@keyval/core'
import { useItem, ItemProvider } from '@keyval/react'

const $swaps = createListApi({ ... })
const swapsApi = createItemApi({ ... })

const SwapWidget = ({ id }) => {
  return (
    <ItemProvider api={swapsItemApi} id={id}>
      <SwapInfo />
      <ConfirmSwapButton />
    </ItemProvider>
  )
}

const SwapInfo = () => {
  const [swapState] = useItem(swapsItemApi)
  
  return <div>{swapState.from} -> {swapState.to}</div>
}

const ConfirmSwapButton = () => {
  const [swapState, api] = useItem(swapsItemApi)
  
  return <button onClick={api.confirm}>Confirm</button>
}

Metadata

Metadata

Assignees

Labels

enhancementNew feature or request

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions