首页指南参考教程
This is documentation for the next SDK version. For up-to-date documentation, see the latest version (SDK 51).

Expo Expo iconExpo

Set of common methods and types for Expo and related packages.

Android
iOS
tvOS
Web

Installation

Terminal
npx expo install expo

API

import * as Expo from 'expo';

Constants

Android
iOS
tvOS
Web

SharedRef

Type: SharedRefType

Hooks

Android
iOS
tvOS
Web

useEvent<TEventsMap, TEventName, TEventListener, TInitialValue>(eventEmitter, eventName, initialValue)

NameTypeDescription
eventEmitterEventEmitter<TEventsMap>

An object that emits events. For example, a native module or shared object or an instance of EventEmitter.

eventNameTEventName

Name of the event to listen to.

initialValue
(optional)
null | TInitialValue

An event parameter to use until the event is called for the first time.

Default: null

React hook that listens to events emitted by the given object. The returned value is an event parameter that gets updated whenever a new event is dispatched.

Returns:

InferEventParameter<TEventListener, TInitialValue>

A parameter of the event listener.

Example

import { useEvent } from 'expo';
import { VideoPlayer } from 'expo-video';

export function PlayerStatus({ videoPlayer }: { videoPlayer: VideoPlayer }) {
  const playerStatus = useEvent(videoPlayer, 'statusChange', videoPlayer.status);

  return <Text>{`Player status: ${playerStatus}`}</Text>;
}

Classes

Android
iOS
tvOS
Web

EventEmitter

A class that provides a consistent API for emitting and listening to events. It shares many concepts with other emitter APIs, such as Node's EventEmitter and fbemitter. When the event is emitted, all of the functions attached to that specific event are called synchronously. Any values returned by the called listeners are ignored and discarded. Its implementation is written in C++ and common for all the platforms.

EventEmitter Methods

Android
iOS
tvOS
Web

addListener<EventName>(eventName, listener)

NameType
eventNameEventName
listenerTEventsMap[EventName]

Adds a listener for the given event name.

Returns:

EventSubscription

Android
iOS
tvOS
Web

emit<EventName>(eventName, ...args)

NameType
eventNameEventName
...argsParameters<TEventsMap[EventName]>

Synchronously calls all of the listeners attached to that specific event. The event can include any number of arguments that will be passed to the listeners.

Returns:

void

Android
iOS
tvOS
Web

listenerCount<EventName>(eventName)

NameType
eventNameEventName

Returns a number of listeners added to the given event.

Returns:

number

Android
iOS
tvOS
Web

removeAllListeners(eventName)

NameType
eventNamekeyof TEventsMap

Removes all listeners for the given event name.

Returns:

void

Android
iOS
tvOS
Web

removeListener<EventName>(eventName, listener)

NameType
eventNameEventName
listenerTEventsMap[EventName]

Removes a listener for the given event name.

Returns:

void

Android
iOS
tvOS
Web

startObserving<EventName>(eventName)

NameType
eventNameEventName

Function that is automatically invoked when the first listener for an event with the given name is added. Override it in a subclass to perform some additional setup once the event started being observed.

Returns:

void

Android
iOS
tvOS
Web

stopObserving<EventName>(eventName)

NameType
eventNameEventName

Function that is automatically invoked when the last listener for an event with the given name is removed. Override it in a subclass to perform some additional cleanup once the event is no longer observed.

Returns:

void

Android
iOS
tvOS
Web

NativeModule

Type: Class extends EventEmitter<TEventsMap>

A class for all native modules. Extends the EventEmitter class.

Android
iOS
tvOS
Web

SharedObject

Type: Class extends EventEmitter<TEventsMap> implements EventEmitter<TEventsMap>

Base class for all shared objects that extends the EventEmitter class. The implementation is written in C++, installed through JSI and common for mobile platforms.

SharedObject Methods

Android
iOS
tvOS
Web

release()

A function that detaches the JS and native objects to let the native object deallocate before the JS object gets deallocated by the JS garbage collector. Any subsequent calls to native functions of the object will throw an error as it is no longer associated with its native counterpart.

Returns:

void

Android
iOS
tvOS
Web

SharedRef

Type: Class extends SharedObject<TEventsMap> implements SharedObject<TEventsMap>

A SharedObject that holds a reference to any native object. Allows passing references to native instances among different independent libraries.

Methods

Android
iOS
tvOS
Web

isRunningInExpoGo()

Returns a boolean value whether the app is running in Expo Go.

Returns:

boolean

Android
iOS
tvOS
Web

registerRootComponent<P>(component)

NameTypeDescription
componentComponentType<P>

The React component class that renders the rest of your app.


Sets the initial React component to render natively in the app's root React Native view on Android, iOS, tvOS and the web.

This method does the following:

  • Invokes React Native's AppRegistry.registerComponent.
  • Invokes React Native web's AppRegistry.runApplication on web to render to the root index.html file.
  • Polyfills the process.nextTick function globally.
  • Adds support for using the fontFamily React Native style with the expo-font package.

This method also adds the following dev-only features that are removed in production bundles.

  • Adds the Fast Refresh and bundle splitting indicator to the app.
  • Asserts if the expo-updates package is misconfigured.
  • Asserts if react-native is not aliased to react-native-web when running in the browser.
Returns:

void

Android
iOS
tvOS
Web

reloadAppAsync(reason)

NameTypeDescription
reason
(optional)
string

The reason for reloading the app. This is used only for some platforms.


Reloads the app. This method works for both release and debug builds.

Unlike Updates.reloadAsync(), this function does not use a new update even if one is available. It only reloads the app using the same JavaScript bundle that is currently running.

Returns:

Promise<void>

Android
iOS
tvOS
Web

requireNativeModule<ModuleType>(moduleName)

NameTypeDescription
moduleNamestring

Name of the requested native module.


Imports the native module registered with given name. In the first place it tries to load the module installed through the JSI host object and then falls back to the bridge proxy module. Notice that the modules loaded from the proxy may not support some features like synchronous functions.

Returns:

ModuleType

Object representing the native module.

Android
iOS
tvOS
Web

requireOptionalNativeModule<ModuleType>(moduleName)

NameTypeDescription
moduleNamestring

Name of the requested native module.


Imports the native module registered with the given name. The same as requireNativeModule, but returns null when the module cannot be found instead of throwing an error.

Returns:

ModuleType | null

Object representing the native module or null when it cannot be found.

Common questions

What if I want to name my main app file something other than App.js?

You can set the "main" in package.json to any file within your project. If you do this, then you need to use registerRootComponent. The export default will not make this component the root of the app if you are using a custom entry file.

For example, you want to make src/main.jsx the entry file for your app and organize all of your app's source code in src directory. You can set this in package.json:

package.json
{
  "main": "src/main.jsx"
}

Then, in src/main.jsx, make sure you call registerRootComponent and pass in the component you want to render at the root of the app:

src/main.jsx
import { registerRootComponent } from 'expo';
import { View } from 'react-native';

function App() {
  return <View />;
}

registerRootComponent(App);
Expo 中文网 - 粤ICP备13048890号