10 Essential React Native Interview Questions *
最好的React Native开发人员和工程师可以回答的全部基本问题. Driven from our community, we encourage experts to submit questions and offer feedback.
Hire a Top React Native Developer NowInterview Questions
The InteractionManager
本机模块是否负责延迟函数的执行,直到“交互”完成. We can call InteractionManager.runAfterInteractions(() => {...})
to handle this deferral. We can also register our own interactions.
InteractionManager
is very important because React Native has two threads. There is a JavaScript UI thread which handles drawing updates to the screen, and another thread used for all tasks not on the UI thread. Since there is only one thread for making UI updates, it can get overloaded and drop frames, especially during things like navigation screen animations. We use the InteractionManager
to ensure that our function is executed after these animations occur so that we do not drop frames on the UI thread. 尝试在动画中绘制一个新屏幕通常是线程无法处理的.
What is wrong with this code for querying a native API?
class GyroJs {
setGyroPosition(pos) {
if (pos === null || typeof pos === 'undefined') {
throw new Error('The position must be defined');
}
this.pos = pos;
}
constructor() {
const gyroscopePosition = NativeModules.MyGyroModule.gyroPosition();
this.setGyroPosition(gyroscopePosition);
}
}
This code will always throw an error because the value of gyroscopePosition
will always be an unresolved Promise
. It’s important to remember that the bridge that connects JavaScript and native code is asynchronous. 我们可以通过传递回调来从这边接收结果(在本例中没有这样做), or by returning a Promise
. In this case, we need to append a then()
call to the gyroPosition()
call and set the position inside it.
React Native之所以大受欢迎,是因为它为许多公司和团队提供了合理的权衡. But building an app in React Native is not always the right choice.
当一个团队正在构建一个不需要极高性能的产品时,React Native是有意义的. The limitations of the asynchronous bridge are a bottleneck for things like 3D games, and games with lots of particle updates. Apps that rely on deep interactions with low-level APIs, or need large amounts of native code, might be easier to build as native apps.
当一个现有的团队已经精通JavaScript和/或有一个现有的React应用程序构建在web或其他平台上时,React Native是有意义的. The “learn once, Facebook所倡导的“到处写”的理念在实现产品跨平台多样化时非常有用. Hiring becomes easier, since JavaScript developers are plentiful, and you don’t need to seek out native specialists.
React Native is partially open source, partially closed source. Facebook维护着一个私有的React Native代码库,用于他们的应用程序. 当私有代码库中的代码可以被分离出来而不包含任何专有代码时, it is often merged into the open source codebase. 这就给React Native的用户留下了开源软件的经典权衡:经常会有bug (React Native仍然处于alpha版本),而且改进可能是参差不齐的. On the other hand, 受到激励的团队可以对源代码做出贡献,并实现他们需要的修复和特性. 根据团队的资源和产品路线图,依赖开源可能是正确的选择.
Apply to Join Toptal's Development Network
and enjoy reliable, steady, remote Freelance React Native Developer Jobs
React Native is built using React. React, at its core, 是否有一个库用于“区分”虚拟DOM并以最少的操作将该DOM呈现到屏幕上. 默认情况下,React不知道它的虚拟DOM树中有哪些节点. Instead, it simply has algorithms that can determine changes in the tree and re-render. React on the web provides its own node primitives ( React Native defines its own primitives (, etc), which are the building blocks for web applications. But new node primitives can be defined, as React Native has done.
,
, etc) which do not render HTML elements but instead map to native views, like UIView
and UIImageView
. 它实现了一个桥接,允许JavaScript运行时与本机运行时进行异步通信. React本身提供了允许React Native工作的树区分和渲染基础设施.
Languages that compile to JavaScript are generally compatible with React Native. React Native使用Babel将JavaScript转换为可被本地操作系统的JavaScript运行时使用的形式, using the react-native
Babel plugin. 只要Babel可以编译你的JavaScript,并且你的代码不依赖于web-或Node.js-specific dependencies, it will run in React Native.
Any pure JavaScript library that does not rely on Node.js runtime modules, and does not rely on web-specific concepts (e.g. window.location.pathname
) will run fine in React Native. 但是我们必须注意,因为没有办法用babel来测试这一点——它不会扫描这些库是否有冒犯性的依赖. A module that uses window.location.pathname
may fail at runtime in an unexpected place.
Bonus: For modules that explicitly rely on Node.js runtime modules, we can often use Babel plugins to browserify these components for use in React Native.
Certain animation types, like Animation.timing
and Animation.spring
, can be serialized and sent across the asynchronous bridge before they are executed. 这允许运行时将动画的实际绘制完全推迟到本机端, 而不是试图发送位置值在桥上的每一帧渲染. This does not work with animations that are not computable ahead of time. For example:
Animated.timing(new Animated.Value(0), {
useNativeDriver: true,
toValue: 100,
duration: 100
}).start()
This will serialize the operation and send it to the native side before starting it.
StyleSheet
是一个模块内置到React Native,允许我们创建不可变的样式表引用. We can pass regular style objects into the create()
method, and the module will freeze the objects, and assign each one an ID. 这有两个好处:它允许我们避免在每次渲染过程中创建一个新的样式对象(这可能导致渲染性能下降)。, 它允许我们通过异步桥只发送一次对象(因为这些样式对象直接映射到本地样式), they need to be passed across).
这种方法的关键缺点是,基于外部标准(如屏幕旋转或甚至用户选择的观看模式)重新计算样式需要构建一些基础设施来确定使用哪种样式. If objects were passed in “raw” they could be recomputed on the fly every time, based on arbitrary criteria.
Imagine you have an app which is a series of lists of images (e.g. like Instagram). The app seems to crash at random. What steps can we take to investigate and mitigate this in React Native?
Often, and especially on the Android platform, lists of images are not properly recycled when scrolling. Their memory is never garbage collected, nor is it manually freed at a lower level. 这将导致内存不足(OOM)崩溃,当应用程序的内存耗尽时,这种崩溃可能会随机发生.
我们可以通过在Xcode或Android Studio中分析应用程序的堆内存使用情况来研究这个问题. 如果您滚动图像列表并注意到堆使用情况稳步攀升而没有下降, it probably means that your images aren’t being recycled properly.
To mitigate this, we can check which list implementation we are using. In modern versions of React Native, ListView
should never be used; ensure that the FlatList
component is handling the rendering instead. 如果调优后这还不够,您可以尝试降低图像的分辨率.
React Native comes with the Animated
API built in. This API is declarative: We define specific animations, using Animated.timing
, Animated.spring
, etc., and provide the exact parameters needed for the animation to run. This technique falls apart when we need lots of subtle and delicate animations on the fly; it’s not performant, and maintaining all that code would be a nightmare.
Instead, we look to the LayoutAnimation
module, which is an interpolative API. We can invoke predefined LayoutAnimation
s, or define our own. LayoutAnimation
watches changes in the positions of elements between cycles of the render loop, and computes the positional differences between elements at different cycles. Then, it interpolates those changes and produces a smooth, natively driven animation.
There is more to interviewing than tricky technical questions, so these are intended merely as a guide. Not every “A” candidate worth hiring will be able to answer them all, nor does answering them all guarantee an “A” candidate. At the end of the day, hiring remains an art, a science — and a lot of work.
Why Toptal
Tired of interviewing candidates? Not sure what to ask to get you a top hire?
Let Toptal find the best people for you.
Hire a Top React Native Developer NowOur Exclusive Network of React Native Developers
Looking to land a job as a React Native Developer?
Let Toptal find the right job for you.
Apply as a React Native DeveloperJob Opportunities From Our Network
Submit an interview question
Submitted questions and answers are subject to review and editing, and may or may not be selected for posting, at the sole discretion of Toptal, LLC.
Looking for React Native Developers?
Looking for React Native Developers? Check out Toptal’s React Native developers.
Marcus Hsu
Marcus has over a decade of experience in front-end development, 在过去的几年里,我一直在用React Native构建React网站,开发iOS和Android应用. 他已经帮助至少31家企业和初创客户使用React设计和构建了高质量的跨平台应用程序, React Native, and Node.js. Marcus还开发了企业级应用,影响了140个国家的3000多万用户.
Show MoreDonnie Waters
Donnie是一位值得信赖的前端工程师,他热衷于学习新事物,同时提供高效的产品. 他有超过六年的软件工程师工作经验,专注于前端的React Native. Donnie developed his own mobile app on the side, amassing over two million downloads.
Show MoreHaleeq Usman
Haleeq是计算机科学领域的领导者,在领导政府机构(如纽约市立大学研究基金会)的工程团队方面拥有多年的经验, top tech private companies, and corporations like Adobe. He is fluent in formal language theory, enabling him to effectively ship programs, apps, and services in any programming language. Haleeq has several years of experience architecting and building enterprise apps, services, and design patterns.
Show MoreToptal Connects the Top 3% of Freelance Talent All Over The World.
Join the Toptal community.