基础
前置课程:JavaScript
想快速熟悉 JavaScript ,推荐阅读 MDN Web Docs 的这两份文档:
Function Components & Class Components
组件有两种写法,classes 或 functions 。
Function Components(推荐写法):
import React from 'react';
import { Text, View } from 'react-native';
const HelloWorldApp = () => {
return (
<View style={{
flex: 1,
justifyContent: 'center',
alignItems: 'center'
}}>
<Text>Hello, world!</Text>
</View>
);
}
export default HelloWorldApp;Class Components :
Native Components & Core Components
React Native 在 runtime 期间为这些组件创建相应的原生的控件。
React Native 内置了一些常用的 Native Components ,它们被称为 React Native 的 Core Components 。开发者也可以根据实际需求构建自己的组件。
相关链接🔗 :所有 Core Components 的文档。
最常用的 Core Components(截图来源):

示例:
各类组件的关系图:
React 基础
React 的核心概念:
components
JSX
props
state
你的第一个组件
示例代码:
这个组件以 function 开头:
Function Components 返回的内容会被渲染成 React element 。这个 Cat 组件将被渲染成 <Text> 元素。
最后,使用 JavaScript 的 export default 导出这个组件。
说明:其他导出组件方式可参考这个 handy cheatsheet on JavaScript imports and exports 。
JSX
React 的官方文档中关于 JSX 的介绍:a comprehensive guide to JSX 。
React 和 React Native 都可使用 JSX ,这种 JavaScript 语法使 React 元素的编写更便利。由于 JSX 是 JavaScript ,因此可以在 JSX 内使用变量,如下面 <Text> 组件使用中的 name 变量(在 JSX 中使用花括号 {} 包裹):
任何 JavaScript 表达式都可放在花括号中执行,如下面代码中的 {getFullName("Rum", "Tum", "Tugger")} 方法调用:
说明:由于 JSX 是包含在 React 库中的,因此需要在文件的开头导入:import React from 'react' 。
自定义组件
可以在自定义的组件中将不同的 Core Components 组合起来,形成一个新的组件,React Native 会一起渲染它们:
在其他组件中可以使用封装好的组件。在下面的 Cafe 组件中,通过 <Cat /> 使用封装好的 Cat 组件:
包含其他组件的称为 parent component 。在上述例子中,Cafe 是 parent component ,Cat 是 child component 。
在 Cafe 组件中可以添加任意多的 Cat 组件。每个 <Cat> 渲染为一个独立的 element ,可以使用 props 来分别定制它们。
Props
Props 是 Properties 的简写。可以使用 Props 自定义 React 组件。例如,在下面的代码中,为每个 <Cat> 组件传入了不同的 name 变量:
React Native 的大部分 Core Components 可以使用 props 来自定义,比如,在使用 Image 组件时,可以传递一个名为 source 的 prop 来指定 image 的来源:
Image 组件有许多不同的 props ,比如 style ,它接受设计和布局相关的属性-值对的 JS 对象 。详情可参考 Image 组件的文档。
说明:
Notice the double curly braces
{{ }}surroundingstyle‘s width and height. In JSX, JavaScript values are referenced with{}. This is handy if you are passing something other than a string as props, like an array or number:<Cat food={["fish", "kibble"]} age={2} />. However, JS objects are also denoted with curly braces:{width: 200, height: 200}. Therefore, to pass a JS object in JSX, you must wrap the object in another pair of curly braces:{{width: 200, height: 200}}
State
props 是用来配置组件的渲染方式的参数,而 state 是组件的 personal data storage 。state 用于数据随时间变化或根据用户的交互而变化的场景。State gives your components memory!
As a general rule, use props to configure a component when it renders. Use state to keep track of any component data that you expect to change over time.
在下面的例子中,猫咖啡馆中的两只猫等着被投喂。它们的饥饿状态以 state 的方式存储,且会在点击“投喂”按钮后发生变化(不同于它们的名字,名字是固定不变的)。
开发者可以调用 React’s useState Hook 来给组件添加 state 。
A Hook is a kind of function that lets you “hook into” React features. For example,
useStateis a Hook that lets you add state to function components. You can learn more about other kinds of Hooks in the React documentation.
示例:
接下来对上述代码进行解读。
首先,需要从 React 中导入 useState :
然后,通过在组件的函数中调用 useState 来声明组件的状态,在这个例子中,useState 创建了一个 isHungry 状态变量:
You can use
useStateto track any kind of data:strings,numbers,Booleans,arrays,objects. For example, you can track the number of times a cat has been petted withconst [timesPetted, setTimesPetted] = useState(0)!
调用 useState 做了两件事情:
创建一个含初值的
state变量;创建一个函数来设置
state变量的值。
命名规则建议:[<getter>, <setter>] = useState(<initialValue>) 。
接下来,添加了 Button Core Component ,并设置了 onPress prop :
当用户点击按钮时,会触发 onPress ,然后调用 setIsHungry(false) ,将 isHungry state 变量设置为 false 。当 isHungry 为 false 时,Button 组件的 disable prop 会被设置成 true ,且 title 也会改变:
You might’ve noticed that although
isHungryis aconst, it is seemingly reassignable! What is happening is when a state-setting function likesetIsHungryis called, its component will re-render. In this case theCatfunction will run again—and this time,useStatewill give us the next value ofisHungry.
最后,把 Cat 组件添加到 Cafe 组件中:
See the
<>and</>above? These bits of JSX are fragments. Adjacent JSX elements must be wrapped in an enclosing tag. Fragments let you do that without nesting an extra, unnecessary wrapping element likeView.
TextInput
TextInput 是一个 Core Component ,它允许用户输入文本。
TextInput有一个onChangeTextprop ,这个 prop 可设置一个函数,这个函数会在 text 发生变化时被调用。TextInput还有一个onSubmitEditingprop ,它携带的函数会在 text 被提交的时候被调用。
示例:
在这个例子中,我们把 text 存储在 state 中,因为它会被用户更改。
相关文档:
ScrollView
下面是一个创建 vertical ScrollView 的示例:
ScrollView 适合用于展示内容有限的页面,因为 ScrollView 中所有的元素都会被渲染,即使它们还没有出现在屏幕上。因此,如果你有一个很长的 list 要展示,你应该使用 FlatList 。
List Views
React Native 提供了一套用于展示列表数据的组件,较常用的是 FlatList 和 SectionList 。
和 ScrollView 组件不同的是,FlatList 组件只会渲染当前展示在屏幕上的元素,而不是一次性渲染所有元素。
FlatList 组件需要两个 prop :data 和 renderItem 。data 是列表的数据源;renderItem 从数据源中取出一项然后返回一个 formatted component 来渲染。
下面的示例使用硬编码数据创建了一个 FlatList 。data 中的每一项被渲染成了一个 Text 组件:
如果想把页面分成多个 section ,类似 iOS 中的 UITableView ,则可以使用 SectionList :
FAQ
Last updated