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

RFC: useItem hook #20

Open
Kelin2025 opened this issue Jul 7, 2023 · 3 comments
Open

RFC: useItem hook #20

Kelin2025 opened this issue Jul 7, 2023 · 3 comments
Assignees
Labels
enhancement New feature or request

Comments

@Kelin2025
Copy link
Member

Kelin2025 commented Jul 7, 2023

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>
}
@Kelin2025 Kelin2025 self-assigned this Jul 7, 2023
@Kelin2025 Kelin2025 added the enhancement New feature or request label Jul 7, 2023
@Minhir
Copy link
Member

Minhir commented Jul 8, 2023

It definitely solves the prop-drilling problem, but there is a new one.
If I'm passing the item from one component to its children, I can guarantee that the item is not null.

const SwapWidget = ({ id }) => {
  const [swapState] = useItem(swapsItemApi, id);

  if (!swapState) {
    return <Placeholder />;
  }

  return (
    <>
      <SwapInfo swapState={swapState} />
    <>
  )
}

const SwapInfo = ({swapState}: {swapState: SwapState}) => {
  return <div>{swapState.from} -> {swapState.to}</div>
}

With provider solution, you need always to check if there is an item in every child.

Or maybe I understand your idea a little bit wrong, and you want to render children conditionally if the item with id exist in kv.

@Kelin2025
Copy link
Member Author

I can guarantee that the item is not null
you need always to check if there is an item in every child

It's up to the user
If they want to pass it through props - they can useItem at top-level and pass it down through props
If they are using useItem (or useItemState or any other solution that takes item from KV) - yes, there's a chance that the item is null. Either handle it or add item! if you're 100% sure it's there
I believe this question is not related to the hook itself. The only difference between useItemState and useItem there is that you get an option to get the key from Context

@Minhir
Copy link
Member

Minhir commented Jul 10, 2023

Sure, we can implement it and see how it helps users in their scenarios.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants