错误集
创建新项目时,本地已经安装好pod,每次都要提示安装pod,然后又安装失败
npx react-native init AwesomeProject
如果本地安装的pod与rn默认的不一致,会报错
Installing dependencies
✔ CocoaPods (https://cocoapods.org/) is not installed. CocoaPods is necessary for the iOS project to run correctly. Do you want to install it? › Yes, with gem (may require sudo)
✔ Installing CocoaPods
✖ Installing CocoaPods dependencies (this may take a few minutes)
✖ Installing CocoaPods dependencies (this may take a few minutes)
error Error: Failed to install CocoaPods dependencies for iOS project, which is required by this template.
Please try again manually: "cd ./Sample3/ios && pod install".
CocoaPods documentation: https://cocoapods.org/
使用跳过install即可
npx react-native init AwesomeProject --skip-install
bundle install
Your Ruby version is 2.6.6, but your Gemfile specified 2.7.5
解决方案:将Gemfile里的版本改成设置提示版本即可
参考:Shell/Bash - 如何全局卸载react native cli
npx react-native init AwesomeProject
报错
,,,,typeError: cli.init is not a function
删除全局的react-native-cli
npm uninstall -g react-native-cli
参考:避免在unmounted组件上调用setState
避免在unmounted的组件上设置state
Warning: Can't perform a React state update on an unmounted component. This is a no-op, but it indicates a memory leak in your application. To fix, cancel all subscriptions and asynchronous tasks in the componentWillUnmount method.
主要场景是一些定时器延迟执行的时候,组件已经删除了还在设置state,
1、class的组件在componentWillUnmount
方法里销毁掉定时器
componentWillUnmount() {
clearTimeout(xx);
}
2、function的组件在useEffect里销毁定时器
useEffect(()=>{
return () =>{ clearTimeout(xx); }
},
[])
在组件还没有加载好就设置内容,常见的场景是设置导航栏右侧的按钮
ExceptionsManager.js:149 Warning: Cannot update a component (
NativeStackNavigator) while rendering a different component (
observerComponent). To locate the bad setState() call inside
observerComponent, follow the stack trace as described in
修改方式,将设置内容放到mounted之后
class组件
componentDidMount() {
this.props.navigation.setOptions({headerRight: (<Button title='AAAA'/>) })
}
function组件
useEffect(() => {
navigation.setOptions({headerRight: (<Button title='AAAA'/>)})
}, []);
网友评论