Giter Club home page Giter Club logo

Comments (15)

rreusser avatar rreusser commented on July 2, 2024 1

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>
    )   
  }
}

from react-native-swiper.

SDLyu avatar SDLyu commented on July 2, 2024

I'm facing same issue

from react-native-swiper.

rreusser avatar rreusser commented on July 2, 2024

Also had to add:

  componentWillReceiveProps(props) {
    if( props.width ) this.setState({ width: props.width || width })
    if( props.height ) this.setState({ height: props.height || height })
  },  

to the swiper module so that changes have an effect. Could just extend Swiper to add these.

from react-native-swiper.

nickmaxwell10 avatar nickmaxwell10 commented on July 2, 2024

I just changed the container style position: 'relative' to flex: 1 in index.js and Voila!

from react-native-swiper.

rreusser avatar rreusser commented on July 2, 2024

@nickmaxwell10 Haha, that's wayyyyy simpler! Oof. A month into RN now and some things that were so difficult have become so much easier than I was making them…

from react-native-swiper.

leecade avatar leecade commented on July 2, 2024

@rreusser yeah flex:1 works fine, RN have a flex-like layout system easy to fit container

from react-native-swiper.

CreativeManix avatar CreativeManix commented on July 2, 2024

I have the same issue, Could you please post a where flex:1 has to be set..?

from react-native-swiper.

lopezjurip avatar lopezjurip commented on July 2, 2024

Same problem here

from react-native-swiper.

rafaelcorreiapoli avatar rafaelcorreiapoli commented on July 2, 2024

Any workarounds on this ?

from react-native-swiper.

rreusser avatar rreusser commented on July 2, 2024

@rafaelcorreiapoli Haven't looked at this in a long time so not sure exactly if/what the flex/positioning fix is, but honestly my approach was to rewrite the part I needed, which was really just the page indicator: https://github.com/rreusser/react-native-paged-scroll-view It's definitely not perfect, but that was my workaround.

from react-native-swiper.

rafaelcorreiapoli avatar rafaelcorreiapoli commented on July 2, 2024

@rreusser Nice! thanks, I will try it out

from react-native-swiper.

rreusser avatar rreusser commented on July 2, 2024

@rafaelcorreiapoli Disclaimer: @leecade's swiper actually works very well, but the flex issue and adding/removing items dynamically and styling things differently meant it was more straightforward to implemented the restricted subset of functionality. So am suggesting mine as just another alternative depending on your needs, but not to take away from this repo. 😄 Also, it seems like I might have a small race condition going on, so I can't guarantee my approach is 100% perfect, but it worked great in production for us. Not working with RN much at the moment, but don't hesitate to let me know if you have issues with it. Good luck!

from react-native-swiper.

kbrk avatar kbrk commented on July 2, 2024

Hi,
There is another approach.
In the index.js I add onLayout event for the component View and then I can set state width and height.

setSize(){
    const w = Dimensions.get('window').width
    const h = Dimensions.get('window').height
    this.setState({width:w, height:h})
}
render() {
..
return(
<View style={[styles.container, {
        width: state.width,
        height: state.height
      }]}
        onLayout={this.setSize.bind(this)}
      >
..
)

This works for me :)

from react-native-swiper.

joehui avatar joehui commented on July 2, 2024

Similar to kbrk's approach, I use the following workaround.

In my code that uses the Swiper, I have the following. Seems to work perfectly for me.

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

	setSize(event){
		const w = event.nativeEvent.layout.width;
		const h = event.nativeEvent.layout.height;
		this.setState({width:w, height:h});
	}

	render() {
		return (
			<View style={{flex:1}}>
				<View style={{flex:0}}>
					... Some content on the header
				</View>
				<View style={{flex:1}} onLayout={this.setSize.bind(this)}>
					<Swiper
						height={this.state.height}
						width={this.state.width}
						...
					>
						... Some content
					</Swiper>
				</View>
			</View>
		);
	}

from react-native-swiper.

narup avatar narup commented on July 2, 2024

@joehui - Your solution worked for me. Thanks

from react-native-swiper.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.