【问题标题】:React native undefined is not a function [duplicate]反应本机未定义不是一个函数[重复]
【发布时间】:2018-10-30 07:00:47
【问题描述】:

我是原生反应的新手。我正在尝试推送到另一个页面。但我说错了

Undefined is not a function(evaluating '_this2._goToProductListing('app.productListing'{title:item.title})')

我的代码是这样的

 _renderContent(section, i, isActive) {
        return (
            <View>
                <List>
                    {section.content.map((item, i) => {
                        return(
                            <ListItem containerStyle={styles.categoryLists}
                                      onPress={() => this._goToProductListing('app.productListing',{title:item.title})}
                                      key={i} title={item.title}
                            />
                        );
                    })}
                </List>
            </View>
        );
    }


    //Send to product list page
    _goToProductListing = (screen,data) => {
        this.props.navigator.push({
            screen: screen,
            title: data.title,
            passProps: {
                data: data
            }
        });
    };
render() {
        return (


            <View style={stylesheet.accrodianWraper}>

                <ScrollView>
                    <Accordion
                        activeSection={this.state.activeSection}
                        sections={CONTENT}
                        touchableComponent={TouchableOpacity}
                        renderHeader={this._renderHeader}
                        renderContent={this._renderContent}
                        duration={0}
                        onChange={this._setSection.bind(this)}
                    />
                </ScrollView>
            </View>
        )
    }

谁能告诉我我做错了什么?我该如何解决?

导航我使用wix react native navigation,页面app.productListing也注册在index.js中。 我正在使用 react-native-collapsible 制作手风琴, 乙

【问题讨论】:

  • 我猜你在这里松散了上下文:renderContent={this._renderContent}
  • 什么意思?手风琴内容正在正确显示。只有当我单击该项目以转到下一页时,才会出现错误而不是将我推到下一页
  • @user7747472 - 查看链接问题的答案。您的 renderContent 是一种方法。 renderContent={this._renderContent} 只是传递函数引用,没有任何指示 this 使用什么。链接的问题答案向您展示了如何确保使用正确的this
  • @T.J.Crowder,非常感谢 TJ 提供的非常有用的链接。我不知道不知道这个。再次感谢你

标签: javascript reactjs react-native wix-react-native-navigation


【解决方案1】:

尝试使_renderContent 也成为具有箭头功能的属性:

_renderContent = (section, i, isActive) => {

【讨论】:

  • 非常感谢。这行得通。我不明白为什么 => 虽然
  • @user7747472 因为在您的示例中,_renderContent 在 Accordion 中被调用,而 Accordion 没有此方法。箭头函数通过将 this 绑定到使用它的上下文来帮助您避免这种情况。
  • 这是一个经常发布的问题,具有可靠的欺骗目标:stackoverflow.com/questions/33973648/… 我们不需要另一个答案。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-05-17
  • 1970-01-01
  • 2018-07-18
  • 1970-01-01
  • 2019-04-13
相关资源
最近更新 更多