Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flow (React Native) is giving me errors for using 'this.state'

Flow is giving me the following error whenever I try to use this.state in my code:

object literal: This type is incompatible with undefined. Did you forget to declare type parameter State of identifier Component?:

Here is the offending code (though it happens elsewhere too):

class ExpandingCell extends Component {     constructor(props) {     super(props);     this.state = {         isExpanded: false     }; } 

Any help would be much appreciated =)

like image 812
Michael Campsall Avatar asked Apr 26 '16 09:04

Michael Campsall


People also ask

Should I use flow in React Native?

Flow will help you prevent bugs and allow for better code documentation among other things. A lot of the React Native documentation and source code already also uses flow, so there has never been a better time to start using it!

How do you handle a state in React Native?

The most common way to set state in React Native is by using React's setState() method. We also have the Context API to avoid prop drilling and pass the state down many levels without passing it to individual children in the tree.

How do you clean state React?

Unfortunately, there isn't a built-in way for us to reset the state of the component to its initial state, so we have to extract the initial state into a variable. When you manage state using the useState hook, all you have to do is call your setState function passing it the initial state.

What happens when state changes in React Native?

state , we use this. setState() . This is a function available to all React components that use state, and allows us to let React know that the component state has changed. This way the component knows it should re-render, because its state has changed and its UI will most likely also change.


2 Answers

You need to define a type for the state property in order to use it.

class ComponentA extends Component {     state: {         isExpanded: Boolean     };     constructor(props) {         super(props);         this.state = {             isExpanded: false         };     } } 
like image 84
edvinerikson Avatar answered Sep 23 '22 21:09

edvinerikson


If you're using flow and want to set this.state in your component's constructor:


1. Create a type for this.state

type State = { width: number, height: number } 

2. Initialize your component with that type

export default class MyComponent extends Component<Props, State> { ... } 

3. Now you can set this.state without any flow errors

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

Here's a more complete example that updates this.state with the width and height of the component when onLayout is called.

// @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',   }, }) 
like image 33
Derek Soike Avatar answered Sep 19 '22 21:09

Derek Soike