ES6 array map doesn't return anything: ReactJS

后端 未结 2 634
慢半拍i
慢半拍i 2020-11-27 22:25

I have an array and i have a simple string value. I want to mapping my array because i\'m try to find my string value.

I have a code like this, but map function doe

2条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-11-27 23:11

    Because you are not returning anything from renderKeywords method, you are returning from map body only. If you don't return anything from function then by default it will return undefined, you need to return the result of map (array of elements).

    Like this:

    renderKeywords() {
        return this.state.question.map((item, key) => {   //here
            return (
                 {item} 
            );
        }); 
    }
    

    In short you can write it like this:

    renderKeywords() {
       return this.state.question.map((item, key) =>  {item}  ); 
    }
    

    Suggestion:

    Assign unique key to each element.

    Check this answer for more details about key: Understanding unique keys for array children in React.js

提交回复
热议问题