Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

139
Vistas
How can I refresh a child element?

I'm currently using this code inside a functional component :

  return (
    <div style={{ height: "700px", overflow: "auto" }}>
      <InfiniteScroll
        pageStart={0}
        loadMore={FetchUpdates}
        hasMore={hasMore}
        loader={
          <ReactLoading type='spin' color='#000000'  />
        }
        useWindow={false}
      >
        <div key={0}>
          {items.map((data) => (
            <Station data={data} func={props.func} />
          ))}
        </div>
      </InfiniteScroll>
    </div>

It works fine when FetchUpdates method is called. But when I'm refreshing the items element from outside, nothing happens.

setItems([]);

Any idea would be welcomed.

Edit : I'm using UseCallback to update values from within the parent element.

  const UpdateItemsAsync = async () => {
    let items2 = await radioBrowser.Next();
    setItems([...items, ...items2]);
    if (items2.length < 50) {
      setHasMore(false);
    }
    else {
      setHasMore(true);
    }
  };

  const FetchUpdates = useCallback(UpdateItemsAsync, [items]);

And a simple call to clear it :

  if (!radioBrowser.IsLastSearch(filter, fname)) {
    radioBrowser.configureSearch(filter, fname);
    setItems([]);
    setHasMore(true);
  }

Edit 2

The full function :

function RadioStations(props) {
  const [items, setItems] = useState(new Array());
  const [hasMore, setHasMore] = useState(true);
  let { filter, fname } = useParams();
  let radioBrowser = new RadioBrowser();

  if (!radioBrowser.IsLastSearch(filter, fname)) {
    radioBrowser.configureSearch(filter, fname);
    setItems([]);
    setHasMore(true);
  }

  const UpdateItemsAsync = async () => {
    let items2 = await radioBrowser.Next();
    setItems([...items, ...items2]);
    if (items2.length < 50) {
      setHasMore(false);
    }
    else {
      setHasMore(true);
    }
  };

  const FetchUpdates = useCallback(UpdateItemsAsync, [items,radioBrowser]);

  return (
    <div style={{ height: "700px", overflow: "auto" }}>
      <InfiniteScroll
        pageStart={0}
        loadMore={FetchUpdates}
        hasMore={hasMore}
        loader={
          <ReactLoading type='spin' color='#000000'  />
        }
        useWindow={false}
      >
        <div key={0}>
          {items.map((data) => (
            <Station data={data} func={props.func} />
          ))}
        </div>
      </InfiniteScroll>
    </div>
  );
}
about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

I may need the entire code structure or working example to answer this in a better way but possibly updating your useCallback can solve this.

Generally whatever variables or functions we use inside useCallback, useEffect or useMemo should be added as a dependency.

const FetchUpdates = useCallback(async() => {
  let items2 = await radioBrowser.Next();
  setItems([...items, ...items2]);
  if (items2.length < 50) {
    setHasMore(false);
  } else {
    setHasMore(true);
  }
}, [items, radioBrowser]);

about 4 years ago · Juan Pablo Isaza Denunciar

0

I've found a workaround for my problem. Here is my solution:

function RadioStations(props) {
  const [items, setItems] = useState(new Array());
  const [hasMore, setHasMore] = useState(true);
 
  let { filter, fname } = useParams();
  let radioBrowser = new RadioBrowser();

  if (!radioBrowser.IsLastSearch(filter, fname)) {
    console.log('ok');
    radioBrowser.configureSearch(filter, fname);
    setItems([]);
    items.length = 0;
    items.init = 1;
    setHasMore(true);
  }

  const FetchUpdates = useCallback(async () => {
    let items2 = await radioBrowser.Next();
    setItems([...items, ...items2]);
    if (items2.length < 50) {
      setHasMore(false);
    }
    else { 
      setHasMore(true);
    }
  }, [items,radioBrowser]);

  return (
    <div style={{ height: "700px", overflow: "auto" }}>
       <InfiniteScroll
        pageStart={0}
        loadMore={FetchUpdates}
        hasMore={hasMore || items.init != undefined}
        loader={
          <ReactLoading type='spin' color='#000000'  />
        }
        useWindow={false}
      >
        <div key={0}>
          {items.map((data) => (
            <Station data={data} func={props.func} key={data.stationuuid}/>
          ))}
        </div>
      </InfiniteScroll>
    </div>
  );
}
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda