TypeError:undefined不是一个对象(评估'this.setState')

问题描述 投票:0回答:1

我想将导出功能之一呈现给我的组件之一(这里是renderHeaderCommentPart到SignIN组件)

如果我直接在SignIn Component中放置跟随已调用模态组件的代码,这是可行的,但是一旦我将另一个组件打包为导出函数,它就无法正常工作并显示错误消息“ TypeError:undefined不是一个对象(求值” this.setState“)

我试图定义setSate但仍然是相同的错误,并且我不必在构造类中使用bind(this),因为这不是事件DOM处理程序

[请帮助我,谢谢!

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

期望:在渲染部分的SignIN组件中

应该显示另一个文件中定义的renderheaderCommentPart函数

reactjs react-native
1个回答
0
投票

这看起来像没有setState()的功能组件;使用回调函数作为在父组件中设置状态的道具。

setState()是类基础组件的异步函数。

© www.soinside.com 2019 - 2024. All rights reserved.