0

I am passing an array of objects as a prop to my Table component, it is supposed to turn every item in the array to a row in a table. But it is not outputting anything...

I have used console.log to successfully look into each item within the array. This is my code:

import React from 'react'; 

export default class Table extends React.Component {
    getTableRow(x) {
        console.log(x); 
        return ( <tr>
                <td>{x.prop1}</td>
                <td>{x.prop2}</td>
                <td>{x.prop3}</td>
        </tr>); 
    }

    resolveInput() {
        //expecting array
        const array = this.props.lastFiveList; 
        //array.map(x => console.log(x)); 
        array.map(x => this.getTableRow(x)); 
    }           

    render() {
        return (
            <table>
             {this.resolveInput()}
            </table>
        ); 
    }
}

I have also tried to bind both functions (same result) and place what getTableRow is supposed to return into a variable and then returned the variable instead of the multiline return(); statement. Furthermore, I tried using forEach instead of map.

0

3 Answers 3

3

You're not returning the array in resolveInput():

    return array.map(x => this.getTableRow(x));
Sign up to request clarification or add additional context in comments.

3 Comments

I'd need more info on the component to help you. Where does this.props.lastFiveList come from?
Nevermind, I resolved it. You were right... My resolveInput was not returning anything. Thank you, Miguel! I think your response came in first, so consider your answer the accepted answer in a few minutes.
@user8951490 Note also that you'll get warnings about trs not being a child of a tbody element, and your trs need keys. Here's a fixed version.
3

resolveInput method is not returning anything. You need to return

resolveInput() {
    //expecting array
    const array = this.props.lastFiveList; 
    //array.map(x => console.log(x)); 
    return  array.map(x => this.getTableRow(x)); 


} 

1 Comment

@GabyakaG.Petrioli thanks for pointing. I updated the mistake.
0

You resolveInput() method is not returning the array that you have created from the map or returning anything for that matter. So try return the new array that you mapped from the array that was passed in.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.