RN实战项目之--GoShopping
一.框架搭建
1.项目搭建 react-native init GoShopping
2.启动服务器:react-native start
3.编译客户端:react-native run-android
4.本项目仿照美团,分为首页,商家,我的,更多四个模块,所以分别新建
Home.js,Shop.js,Mine.js,More.js
使用 react-native-tab-navigator作为导航器,监听tab变化加载不同view,进而完成页面切换
1.资源文件报错:
“FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':app:mergeDebugResources'.
> Error: Some file crunching failed, see logs for details
* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.
BUILD FAILED
Total time: 3.381 secs
Could not install the app on the device, read the error above for details.
Make sure you have an Android emulator running or a device connected and have
set up your Android development environment:
https://facebook.github.io/react-native/docs/android-setup.html
这里报的问题可能是图片资源文件有问题需要排除
2.在使用react-native-tab-navigator时候
React.Children.only expected to receive a single React element child.
onlyChild
index.android.bundle?platform=android&dev=true&hot=false&minify=false:3952:93
render
index.android.bundle?platform=android&dev=true&hot=false&minify=false:55270:48
<unknown>
index.android.bundle?platform=android&dev=true&hot=false&minify=false:20478:27
measureLifeCyclePerf
index.android.bundle?platform=android&dev=true&hot=false&minify=false:19852:14
_renderValidatedComponentWithoutOwnerOrContext
index.android.bundle?platform=android&dev=true&hot=false&minify=false:20477:45
_renderValidatedComponent
index.android.bundle?platform=android&dev=true&hot=false&minify=false:20498:78
performInitialMount
index.android.bundle?platform=android&dev=true&hot=false&minify=false:20066:55
mountComponent
index.android.bundle?platform=android&dev=true&hot=false&minify=false:19983:40
mountComponent
index.android.bundle?platform=android&dev=true&hot=false&minify=false:16251:49
performInitialMount
index.android.bundle?platform=android&dev=true&hot=false&minify=false:20079:48
mountComponent
index.android.bundle?platform=android&dev=true&hot=false&minify=false:19983:40
mountComponent
index.android.bundle?platform=android&dev=true&hot=false&minify=false:16251:49
mountChildren
index.android.bundle?platform=android&dev=true&hot=false&minify=false:19436:56
initializeChildren
index.android.bundle?platform=android&dev=true&hot=false&minify=false:18895:41
mountComponent
index.android.bundle?platform=android&dev=true&hot=false&minify=false:18955:28
mountComponent
index.android.bundle?platform=android&dev=true&hot=false&minify=false:16251:49
performInitialMount
index.android.bundle?platform=android&dev=true&hot=false&minify=false:20079:48
mountComponent
index.android.bundle?platform=android&dev=true&hot=false&minify=false:19983:40
mountComponent
index.android.bundle?platform=android&dev=true&hot=false&minify=false:16251:49
performInitialMount
index.android.bundle?platform=android&dev=true&hot=false&minify=false:20079:48
mountComponent
index.android.bundle?platform=android&dev=true&hot=false&minify=false:19983:40
mountComponent
index.android.bundle?platform=android&dev=true&hot=false&minify=false:16251:49
mountChildren
index.android.bundle?platform=android&dev=true&hot=false&minify=false:19436:56
initializeChildren
index.android.bundle?platform=android&dev=true&hot=false&minify=false:18895:41
mountComponent
index.android.bundle?platform=android&dev=true&hot=false&minify=false:18955:28
mountComponent
index.android.bundle?platform=android&dev=true&hot=false&minify=false:16251:49
performInitialMount
index.android.bundle?platform=android&dev=true&hot=false&minify=false:20079:48
mountComponent
index.android.bundle?platform=android&dev=true&hot=false&minify=false:19983:40
mountComponent
index.android.bundle?platform=android&dev=true&hot=false&minify=false:16251:49
performInitialMount
index.android.bundle?platform=android&dev=true&hot=false&minify=false:20079:48
mountComponent
index.android.bundle?platform=android&dev=true&hot=false&minify=false:19983:40
mountComponent
index.android.bundle?platform=android&dev=true&hot=false&minify=false:16251:49
performInitialMount
index.android.bundle?platform=android&dev=true&hot=false&minify=false:20079:48
mountComponent
index.android.bundle?platform=android&dev=true&hot=false&minify=false:19983:40
mountComponent
index.android.bundle?platform=android&dev=true&hot=false&minify=false:16251:49
performInitialMount
index.android.bundle?platform=android&dev=true&hot=false&minify=false:20079:48
mountComponent
index.android.bundle?platform=android&dev=true&hot=false&minify=false:19983:40
mountComponent
index.android.bundle?platform=android&dev=true&hot=false&minify=false:16251:49
mountChildren
index.android.bundle?platform=android&dev=true&hot=false&minify=false:19436:56
initializeChildren
index.android.bundle?platform=android&dev=true&hot=false&minify=false:18895:41
mountComponent
index.android.bundle?platform=android&dev=true&hot=false&minify=false:18955:28
mountComponent
index.android.bundle?platform=android&dev=true&hot=false&minify=false:16251:49
performInitialMount
index.android.bundle?platform=android&dev=true&hot=false&minify=false:20079:48
mountComponent
index.android.bundle?platform=android&dev=true&hot=false&minify=false:19983:40
mountComponent
index.android.bundle?platform=android&dev=true&hot=false&minify=false:16251:49
mountChildren
index.android.bundle?platform=android&dev=true&hot=false&minify=false:19436:56
initializeChildren
index.android.bundle?platform=android&dev=true&hot=false&minify=false:18895:41
mountComponent
index.android.bundle?platform=android&dev=true&hot=false&minify=false:18955:28
mountComponent
index.android.bundle?platform=android&dev=true&hot=false&minify=false:16251:49
performInitialMount
index.android.bundle?platform=android&dev=true&hot=false&minify=false:20079:48
mountComponent
index.android.bundle?platform=android&dev=true&hot=false&minify=false:19983:40
mountComponent
index.android.bundle?platform=android&dev=true&hot=false&minify=false:16251:49
performInitialMount
index.android.bundle?platform=android&dev=true&hot=false&minify=false:20079:48
mountComponent
index.android.bundle?platform=android&dev=true&hot=false&minify=false:19983:40
mountComponent
index.android.bundle?platform=android&dev=true&hot=false&minify=false:16251:49
performInitialMount
index.android.bundle?platform=android&dev=true&hot=false&minify=false:20079:48
mountComponent
index.android.bundle?platform=android&dev=true&hot=false&minify=false:19983:40
mountComponent
index.android.bundle?platform=android&dev=true&hot=false&minify=false:16251:49
mountComponentIntoNode
index.android.bundle?platform=android&dev=true&hot=false&minify=false:21222:46
perform
index.android.bundle?platform=android&dev=true&hot=false&minify=false:16537:24
batchedMountComponentIntoNode
index.android.bundle?platform=android&dev=true&hot=false&minify=false:21229:22
perform
index.android.bundle?platform=android&dev=true&hot=false&minify=false:16537:24
batchedUpdates
index.android.bundle?platform=android&dev=true&hot=false&minify=false:16062:33
batchedUpdates
index.android.bundle?platform=android&dev=true&hot=false&minify=false:16131:41
renderComponent
index.android.bundle?platform=android&dev=true&hot=false&minify=false:21274:32
render
index.android.bundle?platform=android&dev=true&hot=false&minify=false:7102:42
renderApplication
index.android.bundle?platform=android&dev=true&hot=false&minify=false:46383:21
run
index.android.bundle?platform=android&dev=true&hot=false&minify=false:46171:33
runApplication
index.android.bundle?platform=android&dev=true&hot=false&minify=false:46211:26
__callFunction
index.android.bundle?platform=android&dev=true&hot=false&minify=false:4673:47
<unknown>
index.android.bundle?platform=android&dev=true&hot=false&minify=false:4544:29
guard
index.android.bundle?platform=android&dev=true&hot=false&minify=false:4508:7
callFunctionReturnFlushedQueue
index.android.bundle?platform=android&dev=true&hot=false&minify=false:4543:12
这个问题是由于,react-native-tab-navigator在使用的时候TabNavigator.Item节点间需要有view填充,如下
<TabNavigator.Item
selected={this.state.selectedTab === 'home'}
title="Home"
renderIcon={() => <Image source={...} />}
renderSelectedIcon={() => <Image source={...} />}
badgeText="1"
onPress={() => this.setState({ selectedTab: 'home' })}>
{homeView}
</TabNavigator.Item>
3.效果图如下:
4.错误截图: