Se stai usando flusso e si desidera impostare this.state
in del constructor
componente:
1. Crea una type
per this.state
type State = { width: number, height: number }
2. inizializzare il componente che type
export default class MyComponent extends Component<Props, State> { ... }
3. Ora è possibile impostare this.state
senza flusso errori
constructor(props: any) {
super(props)
this.state = { width: 0, height: 0 }
}
Ecco un altro com esempio completo che aggiorna this.state
con la larghezza e l'altezza del componente quando viene chiamato onLayout
.
// @flow
import React, {Component} from 'react'
import {View} from 'react-native'
type Props = {
someNumber: number,
someBool: boolean,
someFxn:() => any,
}
type State = {
width: number,
height: number,
}
export default class MyComponent extends Component<Props, State> {
constructor(props: any) {
super(props)
this.state = {
width: 0,
height: 0,
}
}
render() {
const onLayout = (event) => {
const {x, y, width, height} = event.nativeEvent.layout
this.setState({
...this.state,
width: width,
width: height,
})
}
return (
<View style={styles.container} onLayout={onLayout}>
...
</View>
)
}
}
const styles = StyleSheet.create({
container: {
display: 'flex',
flexDirection: 'column',
justifyContent: 'center',
alignItems: 'center',
},
})
fonte
2017-12-01 23:49:11
puoi postare le altre funzioni? – QoP