question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Conditional slide rendering

See original GitHub issue

Consider the follow code, mostly from the README. I added a condition render for the first slide.

I will get either a type error in Code Sandbox, or I will get an empty slide on my local clone.

What is the best way to conditionally rendering slides within the carousel?

import React, { Component } from 'react';
import ReactDOM from 'react-dom';
import { Carousel } from 'react-responsive-carousel';

class DemoCarousel extends Component {
    render() {
        return (
            <Carousel>
                {false ? 
                 (<div>
                  <img src="http://lorempixel.com/output/cats-q-c-640-480-1.jpg" />
                  <p className="legend">Legend 0</p>
                  </div>) : null
                }
                <div>
                    <img src="assets/1.jpeg" />
                    <p className="legend">Legend 1</p>
                </div>
                <div>
                    <img src="assets/2.jpeg" />
                    <p className="legend">Legend 2</p>
                </div>
                <div>
                    <img src="assets/3.jpeg" />
                    <p className="legend">Legend 3</p>
                </div>
            </Carousel>
        );
    }
});

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:7 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
MichaelWheldoncommented, Jul 6, 2019

One of your slides is returning null, which will still add a blank slide. You need to filter out (remove) this value before returning. A quick and dirty way off the top of my head would be to:

  • move all your slides into an array and wrap it in a function which removes all null values (lodash’s _.compact and _.map would be handy here, but however you like 🤷‍♂), then map over that array to return each slide. Something like this:
const images = _.compact([{ src: 'img1', legend: 'leg1' }, yourCondition && { src: 'img2', legend: 'leg2 }, { src: 'img3', legend: 'leg3' }]);
const slides = _.map(images, slide => <div><img src={slide.src} /><p>{slide.legend}</p></div>);
...
return <Carousel>{slides}</Carousel>
0reactions
stale[bot]commented, Jul 2, 2021

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Conditional rendering - SlideShare
Condition Rendering is used to represent output by applying conditions in code with three different approached which is fetched while ...
Read more >
Conditionally Rendering Components - Code Like This
Slides. Rendering Components Conditionally. Components can be rendered using If/Else or a Ternary; State within the Class or Function can be used in...
Read more >
How to use Conditional Rendering with Animation in React
In this article we will code together an example of conditional rendering in React with a FadeIn/FadeOut effect.
Read more >
How to animate conditionally-rendered components?
My component renders button or the sidenav depending on the breakWith prop. Clicking on the rendered button causes the SideNav to slide-in ...
Read more >
Conditionally Rendering Components - Bootcamp
React React-Conditional-Rendering. Rendering Components Conditionally. Components can be rendered using If/Else or a Ternary; State within the Class or ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found