react.dev icon indicating copy to clipboard operation
react.dev copied to clipboard

Interface blocking

Open GurovDmitriy opened this issue 1 year ago • 8 comments

From the documentation it follows that it is unnecessary to use state and effect for data transformation, it may be necessary to describe an example when the transformation will block the interface.

  // blocked operation with highweight adapter data
  const dataAdaptive = adapterList(data)

  return <List entity={dataAdaptive} />
  // non blocked with self hook adapter with useEffect and promise
    const adapter = useCallback(
    () => adapterList(data),
    [data],
  )

  const { data: dataAdaptive, pending } = useAdapterData(adapter)

  return <List entity={dataAdaptive} pending={pending} />

GurovDmitriy avatar Dec 01 '23 09:12 GurovDmitriy

@GurovDmitriy Where in the documentation are you referring to? Also, if I am not mistaken handling a heavy async transformation with useState in the main thread (and not useEffect) could still block the interface.

KenFoss avatar Dec 08 '23 14:12 KenFoss

@KenFoss https://react.dev/learn/choosing-the-state-structure#avoid-redundant-state

In my opinion, a whole chapter is needed to explain the calculated state, there you need to write about iterating over a sheet with a lock for a few seconds, and give an example of a solution when useEffect, useDefferedValue and other hooks suitable for such processing are relevant, also indicate which approach to which result will result, given suspense and let's say next.js...

GurovDmitriy avatar Dec 10 '23 17:12 GurovDmitriy

I will work on this, I think it would be a good first issue. I will start with demonstrating how utilizing useState without useEffect, and calling a heavy fetch request will block the interface.

KenFoss avatar Dec 12 '23 05:12 KenFoss

@GurovDmitriy Assign this to me if you can.

KenFoss avatar Dec 12 '23 05:12 KenFoss

@KenFoss yes of course, what should I do? I can make a test environment.

did I explain it correctly? need a demonstration of the following cases

  • long data fetch
  • long adapter of extracted data
  • what hooks and their combination are suitable for this
  • how it connects when working with suspense and or next.js
  • in what cases the interface is not blocked
  • in what cases will we look at the suspense loader

solved cases might look like this

  • long fetch:

    • the skeleton visible / invisible
    • the interface blocked / non blocked
  • long adapter

    • the skeleton visible / invisible
    • the interface blocked / non blocked

here is an example from a real project

after the tutorial it should be clear how to use the adapter, here next.js, urql are used, data extraction is done on the client, after extraction the data is adapted for the component

import { useQuery } from "@urql/next"

export function List() {
  const [{ data, fetching }] = useQuery({
    query: queryList,
  })

  // long time adapter
  const dataAdaptive = adapterList(data.list.data)

  return <List entity={dataAdaptive} loading={fetching} />
}

GurovDmitriy avatar Dec 12 '23 16:12 GurovDmitriy

I don't know what an adapter is in this context, can you provide a full working sandbox?

rickhanlonii avatar Jan 10 '24 20:01 rickhanlonii

const dataFromBackend = [{id: 0, value: 'some'}] const adapter = (dataFromBackend ) => (dataFromBackend .map(item => ({id: item.id, content: item.value})))

only more work and a large list, in real projects such searches take 0.2-0.4sec...

GurovDmitriy avatar Jan 11 '24 15:01 GurovDmitriy

I want to draw your attention to what this conversation is about - while reading the documentation we come across a recommendation to do

const name = useState("Marq") const secondName = useState("Marquez") const fullName = name + secondName

without the caveat that the computed function may block the interface...

GurovDmitriy avatar Jan 11 '24 16:01 GurovDmitriy