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.

Width of slides isn't set if the slide is React component

See original GitHub issue

If I do a regular slide, per docs, everything works fine:

    <Swiper>
      <div className="slide">Awesome slide</div>
      <div className="slide">Awesome slide</div>
      <div className="slide">Awesome slide</div>
    </Swiper>

However, if I try to encapsulate the slide in a component, like so:

        function MyComponent(props) {
          return <div className="slide">Awesome slide</div>
        }
        <Swiper>
          <MyComponent/>
          <MyComponent/>
          <MyComponent/>
        </Swiper>

Then the whole Swiper gets broken - I see all slides at once. It seems to be happening because Swiper doesn’t set the width of the div inside the components in this case.

The only solution I found is something like that:

    <Swiper>
      <div><MyComponent/></div>
      <div><MyComponent/></div>
      <div><MyComponent/></div>
    </Swiper>

I thought that perhaps MyComponent would be getting the width as a prop and I could propagate to its children. However, there is no such prop.

Since I assume the issue stems from the original, non-react, library, how about a fix that does the following:

  • Goes over every provided child
  • Wraps it in a div

Source https://github.com/vitalybe/react-test/tree/react-id-swiper

Screenshot of the issue

image

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:1
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
vitalybecommented, Dec 4, 2017

Yeah, I did it in a more generic way though eventually:

class FixedSwiper extends Component {
  render() {
    return (
      <Swiper>
        {this.props.children.map(child => {
          return <div>{child}</div>;
        })}
      </Swiper>
    );
  }
}

So I can use it like so:

    <FixedSwiper>
      <MyComponent />
      <MyComponent />
      <MyComponent />
    </FixedSwiper>

Source: https://github.com/vitalybe/react-test/blob/2b0c45d08fa5579fdb0b6312fcfa7b518608b2b5/src/App.js

0reactions
dzek69commented, Dec 15, 2017

Yeah, wrapping could cause more issues than it solve. Like making SEO issues worse and I’m trying to fix current SEO issues in this PR: https://github.com/kidjp85/react-id-swiper/pull/120

I do it like @vitalybe and it’s working just fine. I’m rendering div in my component and wrapping them with lis when rendering Swiper children.

Read more comments on GitHub >

github_iconTop Results From Across the Web

The width of react-slick slide does not change when I resize ...
I think I have a problem with re-render element when resizing browser in reactjs :(. Thanks. javascript · reactjs · react-slick · Share....
Read more >
How to create the responsive and swipeable Carousel - Medium
Today I will show you how to build the Carousel, Slider, or Image Gallery component from scratch. If you're not familiar with the...
Read more >
Flickity · Options
Flicking, page dots, and previous/next buttons are mapped to group slides, not individual cells. is-selected class is added to the multiple cells in...
Read more >
Drawer Navigator | React Navigation
Component that renders a navigation drawer which can be opened and closed via ... in initialRouteName prop, if not passed, defaults to the...
Read more >
Documentation - Keen-Slider
If set to auto , the slide size is determined based on the size of there corresponding HTML element. Therefore, the selector -options...
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