0

I have the following code:

state = {
    name: 'Teste II', 
    produto: null,
    alternativas: null,
    loading_produto: true,
    loading_alternativas: true,
    showPopup: false,
};

constructor( props ) {
    super( props );     
}

async componentDidMount(){
    const url = "http://www.mocky.io/v2/5db857da3b0000770035f09b";
    const response = await fetch(url);
    const data = await response.json();
    this.setState({produto: data.suggestions[0], loading_produto: false})
    this.setState({alternativas: data.choices, loading_alternativas: false})
    console.log(this.state.produto.product_data.title);
}

render() {

    const lista = this.state.alternativas;


    if(!this.state.loading_alternativas){

    this.texts = lista.map((text, key) => {
        console.log('chegou aqui');

        <li key={text.id}>{text.name}</li>
    });
}
return (...<ul>{this.texts}</ul>... )

It seems okay for me. Indeed, the code "console.log('chegou aqui');" is being executed. However the command "

    {this.texts}
" is not printing anything.

What is the problem?

Note that the API is being simulated by the link: API

0

3 Answers 3

3

you need to add return

this.texts = lista.map((text, key) => {
    console.log('chegou aqui');

    return (<li key={text.id}>{text.name}</li>)
});
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks! I am starting to use it now.
0

You should add return in the map function.

this.texts = lista.map((text, key) => {
    console.log('chegou aqui');

    return <li key={text.id}>{text.name}</li>
});

Comments

0

Rerendering only happens on state or prop change

You're assigning class attributes to react elements. This is a no go in react. Render from the state like this

<ul>
{
  this.state.alternativas && this.state.alternativas.map((text, key) => <li key={text.id}>{text.name}</li>
}
</ul>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.