I just have a short question about onReady in React Native. I want to use it like this:
<View style={styles.main}><ReelSet ref={(ref) => {this.reelSet = ref;}} onReady={this.onReelsetReady} /></View>
ReelSet is my component. It doesn't come with React Native.
Its has a function called spin (I point out the if-statement below):
spin = () => { this.reelsInMotion = Constants.REELS; for(let i=0; i<Constants.REELS; i++){ this.reels[i].scrollByOffset(this.randomBetween( (Constants.REELS_REPEAT - 6) * this.reels[i].symbols.length, (Constants.REELS_REPEAT - 5) * this.reels[i].symbols.length, ), (reelIdx, results) => { this.reelsInMotion -= 1; this.spinResults[reelIdx] = results; if (this.reelsInMotion === 0){ this.evaluateResults(); this.props.onReady(); //<-- This takes about 2 seconds } }); } }
While this.evaluateResults(); works on time, this.props.onReady(); needs some time. This is because of setState in my onReelsetReady function.
onReelsetReady looks like this:
onReelsetReady = () => { this.setState({ spinButtonActive: true, loadingVisible: false, }) console.log("debug"); }
Why does setState need some time to change the states? I have the same problem here:
this.setState({ credits: this.state.credits - 15, spinButtonActive: false,}, () => { this.reelSet.spin(); console.log("Spin")});
react-native info output:
info Fetching system and libraries information... System: OS: Windows 10 10.0.18362 CPU: (8) x64 Intel(R) Core(TM) i7-6820HQ CPU @ 2.70GHz Memory: 6.78 GB / 15.67 GB Binaries: Node: 12.9.1 - C:\Program Files\nodejs\node.EXE npm: 6.10.2 - C:\Program Files\nodejs\npm.CMD IDEs: Android Studio: Version 3.5.0.0 AI-191.8026.42.35.5900203 npmPackages: react: ~16.9.0 => 16.9.0 react-native: https://github.com/expo/reactnative/archive/sdk37.0.1.tar.gz => 0.61.4
expo --version
3.17.23
If there is any solution, I would be glad if someone could post it.