React.js creating several components with arrays

Asked

Viewed 2,305 times

1

I’m trying to create several components <Pit /> however, one per result coming from the API, without excluding the previously created.

The API returns an object with 2 arrays inside, the solution was to put each array into a state variable and pass as props directly to the Pit component.

However, this way I can create only 1 Pit component and each API return overwrites the previous one...

My code is like this:

myCallback = (dataFromChild) => {
    this.setState({cod_ativo_search: dataFromChild});
    this.callApi(dataFromChild);
}

async callApi(dataFromChild) {
    const response = await api.get(`/ativo/ativo?cod_ativo='${dataFromChild}'`);

    if(response.data[0].valueOf().rows.length != 0 || response.data[1].valueOf().rows.length != 0){

      this.setState({ responseCompra: response.data[0].rows })
      this.setState({ responseVenda: response.data[1].rows })
      this.setState({ codAtivo: dataFromChild })

   }
}

 render() {
    return (
    <div> 
        <SearchBar callbackFromParent={this.myCallback}/>
        <Pit responseCompra={this.state.responseCompra} responseVenda={this.state.responseVenda} codAtivo={this.state.codAtivo} />
      </div>
    );
 }

Any idea how I can create an array and insert each new result coming from the API to create several Pit components?

  • 1

    Place an array of <Pit> data in the state: const dado = {compra: response.data[0].rows, venda: response.data[1]....}; { dados } = this.state; dados.push(dado); this.setState(state => ({state, dados: dados}));. Use a loop to create the Pit: { dados } = this.state; const pits = dados.map(d => <Pit responseCompra={d.compra}... />); return (<div>{pits}</div>);

  • @But the part of creating several Pits worked, but to work I had to do so: const dado = {compra: this.state.responseCompra, venda: this.state.responseVenda}; &#xA;this.state.dados.push(dado);&#xA;&#xA;const pits = this.state.dados.map(d => <Pit responseCompra={d.compra} responseVenda={d.venda} />); Only dados in state is duplicating and creating duplicate cards, follows image of how is getting duplicated state state_duplicated What is wrong?

  • @Mari found a solution, your reply helped a lot, write in the reply to mark as solution please, I add what I did in the comment.

2 answers

2


Place an array of data in the state:

const dado = {compra: response.data[0].rows, venda: response.data[1]....}; 
const { dados } = this.state; 
dados.push(dado); 
this.setState(state => ({state, dados: dados}));. 

And in render use a loop to create the Pit:

const { dados } = this.state; 
const pits = dados.map(d => <Pit responseCompra={d.compra}... />); 
return (<div>{pits}</div>);

See working here

1

It worked, I just needed a few tweaks:

const dado = {
      compra: this.state.responseCompra,
      venda: this.state.responseVenda
};

this.state.dados.push(dado);

in yielding

const pits = this.state.dados.map((d, i) => {
            return (
                <Pit
                    key={i} 
                    compra={d.compra} 
                    venda={d.venda}  />
            );
        })

return(
   <SearchBar callbackFromParent={this.myCallback}/>
   <div>{pits}</div>
)
  • You should not change the state by changing objects directly. You should always use setState. It is also not necessary to set buy and sell in the state to then add in the data array (I will update my reply with a complete code to illustrate better).

  • Now I ask =) What is this callbackFromParent?

  • It is a prop passed to the Searchbar component, which receives the value of the search, makes a process and returns so this.props.callbackFromParent(this.state.ValorPesquisa.toUpperCase()); for the function myCallback of the component that made the call.

  • Thanks for the answer and for the new example, I will review my code =)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.