Code / JavaScript Flashcards
What is the difference between var
, let
and const
?
SCOPE:var
is scoped to a function, while const
and let
are scoped to a block.
DECLARATION:const
can only be declared once!
What is the command to create a React Native app?
$ npx react-native init AppName –verbose –template react-native-template-typescript
What is the command to run a React Native app on Android Device?
$ cd APP_NAME
$ npx react-native run-android –deviceId=DEVICE_ID –verbose
What is the React Component Lifecycle?
Mounting Cycle:
- constructor(object props)
- componentWillMount()
- render() -> React Element
- componentDidMount()
Updating Cycle:
- componentWillReceiveProps(object nextProps)
- shouldComponentUpdate(object nextProps, object nextState) -> boolean
- componentWillUpdate(object nextProps, object nextState)
- render() -> React Element
- componentDidUpdate(object prevProps, object prevState)
What is the role of constructor()
?
- The component class is instantiated.
- The parameters to the constructor are the element’s initial
props
, as specified by the parent element. - Optionally, we can specify an initial state for the element by assigning an object to
this.state
. - At this point, no native UI has been rendered yet for this element.
What is the role of componentWillMount()
?
- Invoked only once, before rendering occurs for the first time.
- At this point, there is still no native UI rendered for this element.
What is the role of render()
(Mounting Cycle)?
The render method must return a React Element to render (or null, to render nothing).
What is the role of componentDidMount()
?
- Invoked only once, after rendering occurs for the first time.
- At this point, the native UI for this element has finished rendering, and may be accessed through
this.refs
for direct manipulation. - If you need to make async API calls or execute delayed code with
setTimeout
, that should generally be done in this method.
What is the role of componentWillReceiveProps()
?
The parent of this Component has passed a new set of props
. This component will re-render.
You may optionally call this.setState()
to update this component’s internal state
before the render
method is called.
What is the role of shouldComponentUpdate()
?
- Based on the next values of
props
andstate
, a component may decide to re-render or not to re-render. - The base class’s implementation of this method always returns
true
(the component should re-render). - For optimization, override this method and check if either
props
orstate
have been modified, e.g. run an equality test of each key/value in these objects. - Returning
false
will prevent therender
method from being called.
What is the role of componentWillUpdate()
?
- Invoked, after the decision has been made to re-render.
2. You may not call this.setState() here, since an update is already in progress.
What is the role of render()
(Updating Cycle) ?
- This method is called, assuming
shouldComponentUpdate
returnedtrue
. - The render method must return a React Element to render (or null, to render nothing).
What is the role of componentDidUpdate()
?
- This method is invoked after re-rendering occurs.
- At this point, the native UI for this component has been updated to reflect the React Element returned from the
render()
method.
What is a View
component in React Native?
-
View
s are the most basic building blocks of React Native app, much likediv
s. - In terms of implementation, View is an abstraction layer on top of the target platform’s native equivalent, whether that’s
UIView
,android.view
,<div>
, or something else.</div>