React Native官網(wǎng)上面關(guān)于TextInput的文章
importReact, { Component }from'react';
import{ AppRegistry, Text, TextInput, View }from'react-native';
classPizzaTranslatorextendsComponent{constructor(props) {super(props);this.state = {text:''}; } render() {return( this.setState({text})} /> {this.state.text.split(' ').map((word)=>word &&'??').join(' ')} ); }}
//注冊(cè)應(yīng)用(registerComponent)后才能正確渲染//注意:只把應(yīng)用作為一個(gè)整體注冊(cè)一次,而不是每個(gè)組件/模塊都注冊(cè)
AppRegistry.registerComponent('PizzaTranslator',()=>PizzaTranslator);
this.state.text.split(' ').map((word)=>word && '??').join(' ');
這句話的意思是把text這個(gè)字符串按照空格劃分成數(shù)組,再將數(shù)組里面的每一個(gè)元素都放到(word)=>word && '??'這個(gè)函數(shù)里面處理,從而得到一個(gè)新的數(shù)組,再將所有這個(gè)新的數(shù)組里面的元素按照空格連接起來組成一個(gè)新的字符串。
等價(jià)于
this.state.text.split(' ').map((word)=>func(word)).join(' ');
function func(word){
if(word)
return '??';
return word;
}