Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

156
Visualizações
ReactJS: Select with filter data

I'm creating a select with data recovered from two api calls.

I call the first api to obtain all result of people.

I call the second api (with pagination, so I make any time the page number to obtain every time 20 more data).

Now from the result of the second api call I should disable or not show the same results as the first api call.

This is my code:

const SelectAsyncPaginatePeople = props => {
  const [listPeople, setListPeople] = useState(null)
 
  useEffect(() => {
    findListPeople()
  }, [])

  const findListPeople = async () => {
    const response = await fetch(`${apiCall1}`)
    const optionListPeople = await response.json();
    setListPeople(optionListPeople);
  }

  const loadOptions = async (searchQuery, loadedOptions, { page }) => {
    const response = await fetch(
      `${apiCall2}?page=${page}&size=20&deletedDate.specified=false${searchQuery.length >= 3 ? `&personName.contains=${searchQuery}` : ''
      }`
    );
    const optionPerson = await response.json();


    let optionToShow = []

    optionToShow.push(...optionToShow, ...optionPerson.filter((re) => !listPeople.some((rent) => { return re.peopleID === rent.personID })))

    return {
      options: optionToShow,
      hasMore: optionToShow.length >= 1,
      additional: {
        page: searchQuery ? 2 : page + 1,
      },
    };
  };

  const onChange = option => {
    if (typeof props.onChange === 'function') {
      props.onChange(option);
    }
  };

  return (
    <AsyncPaginate
      value={props.value}
      loadOptions={loadOptions}
      getOptionValue={option => option.personID}
      getOptionLabel={option => option.personName}
      onChange={onChange}
      isClearable={true}
      isSearchable={true}
      placeholder="Select Person"
      additional={{
        page: 0,
      }}
    />
  );
};

Now in this way I obtain the first 20 data from the apicall2, and I filter with the result from the apiCall1.

But the problem is when I call second page (for the apiCall2), the data aren't filtered another time with the data of apiCall1.

What I would to obtain:

ApiCall1: dataApiCall1,
ApiCall2 (with page: 1) : dataApiCall2(first 20 data)
dataApiCall2 - dataApiCall1 = dataNotIncludedinApiCall1 // and this work.
ApiCall2 (with page: 2) : dataApiCall2(other 20 data)
dataApiCall2 - dataApiCall1 = dataNotIncludedinApiCall1

...

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

It's bit hard to tell from the code you posted, without more context. It's probably because you're not updating the state in loadOptions

Besides, you shouldn't really use push(), especially in React. Try to not use mutable methods. The lines:

let optionToShow = []

 optionToShow.push(...optionToShow, ...optionPerson.filter((re) => !listPeople.some((rent) => { return re.peopleID === rent.personID })))

Should be refactored to

let optionToShow = [...new Map([...optionPerson, ...listPeople].map((item, key) => [item[key], item])).values()]; 

You can find an excellent explanation of the above snippet here

Albeit for the above to work you should make sure that the object keys are the same. (Even if you're not going to use that snippet, you should do it. It's good to keep your code consistent).

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda