如何解决 React Native 的警告信息
Posted
技术标签:
【中文标题】如何解决 React Native 的警告信息【英文标题】:How to solve the warning message of React Native 【发布时间】:2018-08-11 07:38:21 【问题描述】:我现在正在学习 React Native,最近收到了下面的日志消息。
警告:componentWillMount 已弃用,将在 下一个主要版本。请改用 componentDidMount。作为临时 解决方法,您可以重命名为 UNSAFE_componentWillMount。
我正在学习本教程“React Native 教程:使用 javascript 构建 android 应用” https://www.raywenderlich.com/178012/react-native-tutorial-building-android-apps-javascript
我应该怎么做才能删除消息?
我安装了 react-native-cli 并做了 react-native init projectName。 我更改了 package.json。 我将“react”:“^16.3.0-alpha.1”更改为“^16.2.0”,然后我进行了 npm install。
我的 package.json
"name": "PropertyFinder", "version": "0.0.1", "private": true, “脚本”: “开始”:“节点 node_modules/react-native/local-cli/cli.js 开始”, “测试”:“开玩笑”,“依赖”: “反应”:“^16.2.0”, “反应原生”:“0.54.0”, “反应导航”:“^1.3.0”,“devDependencies”: "babel-jest": "22.4.1", “babel-preset-react-native”:“4.0.0”, “笑话”:“22.4.2”, "react-test-renderer": "^16.2.0" , "jest": “预设”:“反应原生”
但仍显示该警告。
SearchPage.js
'use strict';
import React, Component from 'react';
import
StyleSheet,
Text,
TextInput,
View,
Button,
ActivityIndicator,
Image
from 'react-native';
// import StackNavigator from 'react-navigation';
export default class SearchPage extends Component<>
static navigationOptions =
title: 'Property Finder',
;
constructor(props)
super(props);
this.state =
searchString: 'london'
;
_onSearchTextChanged = (event) =>
console.log('_onSearchTextChanged');
this.setState(
searchString: event.nativeEvent.text
);
console.log('Current: ' + this.state.searchString + ', Next: ' + event.nativeEvent.text);
render()
console.log('SearchPage render');
return (
<View style=styles.container>
<Text style=styles.description>
Search for houses to buy!
</Text>
<Text style=styles.description>
Search by place-name or postcode.
</Text>
<View style=styles.flowRight>
<TextInput
underlineColorAndroid='transparent'
style=styles.searchInput
value=this.state.searchString
onChange=this._onSearchTextChanged
placeholder='Search via name or postcode' />
<Button
onPress=() =>
color='#48bbec'
title='Go'
/>
</View>
<Image source=require('./Resources/house.png') style=styles.image />
</View>
);
const styles = StyleSheet.create(
description:
fontSize: 18,
textAlign: 'center',
color: '#656565',
marginBottom: 20,
,
container:
padding: 30,
marginTop: 65,
alignItems: 'center',
,
welcome:
fontSize: 20,
textAlign: 'center',
margin: 10,
,
instructions:
textAlign: 'center',
color: '#333333',
marginBottom: 5,
,
flowRight:
flexDirection: 'row',
alignItems: 'center',
alignSelf: 'stretch',
,
searchInput:
height: 36,
padding: 4,
marginRight: 5,
flexGrow: 1,
fontSize: 18,
borderWidth: 1,
borderColor: '#48bbec',
borderRadius: 8,
color: '#48bbec',
,
image:
width: 217,
height: 138,
,
);
App.js
'use strict';
/**
* Sample React Native App
* https://github.com/facebook/react-native
* @flow
*/
import React, Component from 'react';
import
Platform,
StyleSheet,
Text,
View
from 'react-native';
import StackNavigator from 'react-navigation';
import SearchPage from './SearchPage';
// type Props = ;
const App = StackNavigator(
Home:
screen: SearchPage,
,
);
export default App;
我的英语不好,对不起。
【问题讨论】:
【参考方案1】:enter link description here
React v16.3之后,针对生命周期功能进行了调整,那就熟悉一下吧。
【讨论】:
以上是关于如何解决 React Native 的警告信息的主要内容,如果未能解决你的问题,请参考以下文章
如何在 React Native iOS 模拟器中隐藏警告?
React Native 如何解决Encounted two children with the same key,"::"....
ESLint 警告;已定义但从未用于 react-native 组件 [重复]