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.

Doesn't fit inside container unless width/height are hard-coded

See original GitHub issue

I’d like the Swiper to take up the full size of its parent View element. Approaches:

  1. If I don’t specify width and height attributes, the Swiper element extends outside the parent container to full-screen (gets size from device properties?).
  2. Styling the container doesn’t appear to have any effect.
  3. If I hard-code width and height attributes it will sit inside the container, but I don’t want to hard-code these numbers.

If the container is indeed flex:1, I’d expect it to expand to the parent container size rather than the full screen dimensions. Is there a way to achieve this?

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Comments:15 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
rreussercommented, Sep 22, 2015

Okay, so this is horrendously ugly on multiple fronts, but depending on how many times you need to do this… This does work for me… I’m basically just measuring the parent container after it renders (plus a timeout), then just hard-coding it anyway. It’s really not great. Forgive the ES6/7 notation, but it should be reasonably straightforward. Let me know if not. Or if you have a better solution.

export default class ItemSwiper extends React.Component {

  constructor(props) {
    super(props)
    this.state = { width: null, height: null }
  }

  componentDidMount() {
    setTimeout(this.measureSwiper)
  }

  storeSwiperLayout = (ox, oy, width, height, px, py) => this.setState({ width: width, height: height })

  measureSwiper = () => this.refs.sillyWrapper.measure(this.storeSwiperLayout);

  renderSwiper = () => {
    if( this.state.width > 0 ) { 
      return (
        <Swiper style={styles.swiper} width={this.state.width} height={this.state.height}>
          { this.props.items.map(i => this.renderItem(i)) }
        </Swiper>
      )   
    }   
  }

  render() {
    return (
      <View style={styles.sillyWrapper} ref="sillyWrapper">
        { this.renderSwiper() }
      </View>
    )   
  }
}
0reactions
narupcommented, Jul 29, 2017

@joehui - Your solution worked for me. Thanks

Read more comments on GitHub >

github_iconTop Results From Across the Web

Avoid hardcoding container width - css - Stack Overflow
I'm setting up a grid of images with flex flow, wrap-reverse. Finally got the container setup to overflow, but I had to artificially ......
Read more >
Fluid Width Video | CSS-Tricks
Declaring static widths isn't a good idea in fluid width environments. What if the parent container for that video shrinks narrower than the ......
Read more >
Intrinsic Sizing - SVG
Firstly, CSS' object-fit sizing determines the concrete object size of the replaced content which is possibly different to the CSS box the replaced...
Read more >
Setting Height And Width On Images Is Important Again
To prevent layout shifts and improve performance scores, we need to always set width and height attributes on our images.
Read more >
fit-content - CSS: Cascading Style Sheets - MDN Web Docs
The fit-content behaves as fit-content(stretch). In practice this means that the box will use the available space, but never more than ...
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