React Native页面跳转报错:undefined is not a function 求解决
问题排查与修复方案
让我帮你搞定这个报错,看下来核心问题和修复步骤如下:
1. 直接触发报错的语法错误
你的报错信息里显示调用代码是:_this2._goToProductListing('app.productListing'{title:item.title})——这里两个参数'app.productListing'和{title:item.title}之间少了一个逗号!这是JS语法的硬错误,引擎根本没法正确解析这个函数调用,才会抛出"Undefined is not a function"的异常(解析失败后,表达式被错误识别为非函数类型的调用)。
赶紧修复_renderContent里的onPress回调,补上逗号:
<ListItem containerStyle={styles.categoryLists} onPress={() => this._goToProductListing('app.productListing', {title: item.title})} // 这里补上逗号! key={i} title={item.title} />
2. 额外保障:确认this上下文绑定
虽然你用箭头函数定义了_goToProductListing,但为了避免map回调里的this指向问题,建议把_renderContent也改成箭头函数(或者在constructor里手动绑定),这样能彻底确保this指向组件实例:
方案A:改成箭头函数
// 把_renderContent改成箭头函数 _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> ); }
方案B:在constructor里绑定
constructor(props) { super(props); this.state = { activeSection: null }; // 绑定_renderContent的this this._renderContent = this._renderContent.bind(this); }
3. 验证Wix导航的配置正确性
最后再确认下导航相关的配置,确保万无一失:
- 检查
index.js里是否正确注册了目标页面:
// 确保注册代码类似这样 Navigation.registerComponent('app.productListing', () => ProductListingScreen);
- 确认当前组件是通过Wix导航栈加载的,这样
this.props.navigator才会是有效的导航实例(如果是直接渲染的组件,navigator不会自动传入props)。
先修复第一个语法错误,这是最紧急的问题,之后再验证其他配置,应该就能顺利跳转到商品列表页了!
内容的提问来源于stack exchange,提问作者user7747472
相关产品推荐
相关产品推荐

