0

I have a map function that is returning results in the console, however it's not rendering in the DOM.

Here is my map function:

const featuredMakes = makes.filter(makes => makes.featured === true);
    var featured = Object.keys(featuredMakes).map(function(s){ 
        console.log(featuredMakes[s].title);  
        return (
            <Col className="mb-3">
                <Link
                    key={featuredMakes[s].title}
                    href={{ pathname: '/deal-list', query: query }}
                    as={{ pathname: '/filter', query: query }}
                    passHref
                >
                    <a>
                        <img
                            style={{ height: '80px', width: '80px' }}
                            src={featuredMakes[s].logo}
                            alt={featuredMakes[s].title + ' logo'}
                        />
                    </a>
                </Link>
            </Col>
        );
    });

And here is what's in my render call:

<Row>{this.renderFeaturedMakes(makes)}</Row>

The console is showing me everything correctly, however nothing is showing in the DOM. I am still learning React and ES2016. Any insight is helpful and appreciated!

4
  • 1
    And what is renderFeaturedMakes? Commented Nov 14, 2018 at 13:22
  • 1
    are you returning featured variable from renderFeaturedMakes function Commented Nov 14, 2018 at 13:22
  • Also if makes is an array, how is featuredMakes an object Commented Nov 14, 2018 at 13:23
  • Sorry renderFeaturedMakes is the name of my function from the first code block. featuredMakes is returning the markup for each object. Commented Nov 14, 2018 at 13:26

2 Answers 2

1

The solution to show them on the browser is to add return statement in renderFeaturedMakes method. Otherwise this method doesn't return nothing.

And if you want to write them in ES2016, it might help you.

const featuredMakes = makes.filter(makes => makes.featured === true);
const featured = Object.keys(featuredMakes).map(s => (
    <Col className="mb-3">
        <Link
            key={featuredMakes[s].title}
            href={{ pathname: '/deal-list', query: query }}
            as={{ pathname: '/filter', query: query }}
            passHref
        >
            <a>
                <img
                    style={{ height: '80px', width: '80px' }}
                    src={featuredMakes[s].logo}
                    alt={featuredMakes[s].title + ' logo'}
                />
            </a>
        </Link>
    </Col>
));
return featured;

// or just return instead of inserting `featured` variable
// return Object.keys(featuredMakes).map(s => { 
Sign up to request clarification or add additional context in comments.

1 Comment

Much cleaner. Thank you!
0

I ended up getting it. I was returning from a var, when I needed to return directly. Line 2 from above:

return Object.keys(featuredMakes).map(function(s){ 
        // console.log(featuredMakes[s]);  
        return (

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.