简体   繁体   English

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

[英]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)我想将 export function 之一渲染到我的组件之一(这里是 renderheaderCommentPart 到 SignIN 组件)

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")如果我将调用模态组件的后续代码直接放在 SignIn 组件中,这是有效的,但是一旦我将另一个组件打包为导出 function,它就不起作用并显示错误消息“TypeError:undefined is not an object(evaluation”this.设置状态")

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

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期望:在渲染部分的 SignIN 组件中

should be show the renderheaderCommentPart function as defined in another file应该显示另一个文件中定义的 renderheaderCommentPart function

This looks like functional component which has not setState();这看起来像没有 setState() 的功能组件; use callback function as a props to set state in the parent component.使用回调 function 作为道具在父组件中设置 state。

setState() is asynchronous function of class base component. setState() 是 class 基本组件的异步 function 。

or use Hooks if your project react version 16.8.或者如果您的项目反应版本 16.8,则使用 Hooks。 and above以上

Or convert your component as class base或将您的组件转换为 class 基础

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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