简体   繁体   中英

TypeError: undefined is not an object(evaluation'this.setState')

I want to render one of export function to one of my component ( here is renderheaderCommentPart to SignIN component)

This is working if I put follow code which has called modal component directly inside SignIn Component, but once I packed in another component as export function, it is not working and show error message " TypeError: undefined is not an object(evaluation "this.setState")

I tried to defined the setSate but still the same error, and I do not have to use bind(this) in construct class cause this is not event DOM handler

Please help me, thank you!!

 export function renderheaderCommentPart(onPress, hearderLeftText, modalContent)
{
   this.setState({
       visible: true,
       swipeableModal: false
   });
   return (
        <View>
            <Header style={Styles.header}>
                  <Left>
                    <Button transparent  onPress={onPress}>
                      <Icon name='ios-arrow-back'/>
                    </Button>
                  </Left>
                  <Body>
                    <Title style={Styles.headerText}>{hearderLeftText}</Title>
                  </Body>
                  <Right>         
                    <View>
                     <Button transparent
                       onPress={() => {
                          this.setState({ visible: true });
                        }}
                     >
                      <Icon style={Styles.icon_more} name='more' />
                    </Button>
                     <Modal
                        visible={this.state.visible}
                        onTouchOutside={() => {
                                  this.setState({ visible: false });
                                }}
                        footer={
                          <ModalFooter>
                            <ModalButton
                              text="CANCEL"
                              onPress={() => {
                                      this.setState({ swipeableModal: false });
                                    }}
                            />
                            <ModalButton
                              text="OK"
                              onPress={() => {}}
                            />
                          </ModalFooter>
                        }
                      >
                        <ModalContent>
                          <Text>{modalContent}</Text>
                        </ModalContent>
                      </Modal>
                    </View>
                  </Right>
                </Header>
            </View>
     )
}

Expect: in SignIN component in render part

should be show the renderheaderCommentPart function as defined in another file

This looks like functional component which has not setState(); use callback function as a props to set state in the parent component.

setState() is asynchronous function of class base component.

or use Hooks if your project react version 16.8. and above

Or convert your component as class base

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM