Chapter Three - React Native Framework and Components
Chapter Three - React Native Framework and Components
List Views - unlike ScrollView (more generic), list view components only render(provide)
elements that are currently showing on the screen. Best to display longlist of data.
Android-specific components and API’s - Many of the following
components provide wrappers for commonly used Android classes.
Others - These components may be useful for certain applications.
Common Core Components used
Running your first react native - app
Demo - https://reactnative.dev/docs/intro-react
React Native - fundamentals
React Native runs on React, a popular open source library for
building user interfaces with JavaScript.
So it is important to understand React itself first.
The following are core concepts behind React and to work with React
native:
components
JSX
props
state
Components
A component is like a blueprints.
It can be created as Function Component or Class Component.
We focus on Function Component
Let us define our own function component – HelloReact
To define, first use JavaScript to import React and React Native core
components.
Note that there are many ways to export your component. Depending on your
app’s file structure, you might need to use a different convention.
JSX
Now take a closer look at that return statement. <Text>Hello, I am your cat!
</Text> is using a kind of JavaScript syntax that makes writing elements
convenient: JSX.
React and React Native use JSX, a syntax that lets you write elements inside
JavaScript:
<Text>Hello, I am react native!</Text>.
Because JSX is JavaScript, you can use variables inside it. Declaring a name
for the HelloReact, name, and embedding it with curly braces inside <Text>.
const name = “react";
<Text>Hello, I am {name} !</Text>
Any JavaScript expression will work
between curly braces, including
function calls:
{getFullName(“Lucy", “Afar")}
The curly braces is like creating a
portal into JS functionality in your
JSX!
JSX is included in react library.
React lets you nest components inside each
other to create new components.
Nestable and reusable components are at the
heart of the React paradigm.
Text and TextInput are nested inside a View , and
React Native will render them together.
In React Native, View uses Flexbox for its
children’s layout.
Component can be render multiple times
and in multiple places without repeating
your code by using <Hello/>
Any component that renders other
components is a parent component.
Here, MultiGreetings is a parent
component and each Hello is a child
component.
Each <Hello> renders a unique element
—which you can customize with props.
Props – short for “properties”
Props is short for “properties”.
Props let you customize React
components.
E.g. you pass each <Student> a
different name for Student to render:
Most of React Native’s Core
Components can be customized with
props, too.
E.g. when using Image, you pass it a
prop named src to define what image it
shows.
Image has many different props,
including style, which accepts a JS
object of design and layout related
property-value pairs.
Notice
the double curly braces {{ }} surrounding style‘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:
<Student names={["Muna", "Kirubel"]} age={18} />.
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}}
You can build many things with props and the Core Components (Text,
Image, and View). But to build something interactive, you’ll need state.
State
While you can think of props as arguments you use to configure how
components render, state is like a component’s personal data storage.
State is useful for handling data that changes over time or that comes
from user interaction.
State gives your components memory!
Note - 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.
You can add state to a component by calling React’s useState Hook.
A Hook is a kind of function that lets you “hook into” React features.
For example, useState is a Hook that lets you add state to function
components.
The example here takes place in a cat
cafe where two hungry cats are waiting
to be fed.
Their hunger, which we expect to
change over time (unlike their names),
is stored as state.
To feed the cats, press their buttons—
which will update their state.
First, you will want to import useState
from React.
10/10/2023
10/10/2023
If the default Button component does not suit your needs, you can use one of
the following components instead.
Touchable Opacity
This element will change the opacity of an element when touched.
Touchable Highlight
When a user presses the element, it will get darker and the underlying color will show
through.
Touchable Native Feedback
This will simulate ink animation when the element is pressed.
Touchable Without Feedback
This should be used when you want to handle the touch event without any animation.
Usually, this component is not used much.
10/10/2023
TouchableOpacity
10/10/2023
Touchable Highlight
10/10/2023
Touchable Native Feedback
10/10/2023
Touchable Without Feedback
10/10/2023
Switch
Renders a boolean input.
This is a controlled component that requires an onValueChange
callback that updates the value prop in order for the component to
reflect user actions.
If the value prop is not updated, the component will continue to
render the supplied value prop instead of the expected result of any
user actions.
Styling
React Native style your application using JavaScript.
All core components accept a prop named style. The style names and values
usually match how CSS works on the web, except names are written using
camel casing, e.g. backgroundColor rather than background-color.
The style prop can be a plain old JavaScript object. That's what we usually
use for example code. You can also pass an array of styles - the last style in
the array has precedence, so you can use this to inherit styles.
As a component grows in complexity, it is often
cleaner to use StyleSheet.create to define several
styles in one place.
In the following example, the red, yellow, and green views are all
children in the container view that has flex: 1 set. The green view uses
flex: 1 , the yellow view uses flex: 2, and the red view uses flex: 3 .
1+2+3 = 6, which means that the green view will get 1/6 of the space,
the yellow 2/6 of the space, and the red 3/6 of the space.
See the code and the output in the next slid.
Flex Direction
flexDirection controls the direction in which the children of a node are laid out. This is also referred to as
the main axis. The cross axis is the axis perpendicular to the main axis, or the axis which the wrapping
lines are laid out in.
column (default value) Align children from top to bottom. If wrapping is enabled, then the next line will start to the
right of the first item on the top of the container.
row Align children from left to right. If wrapping is enabled, then the next line will start under the first item on the
left of the container.
column-reverse Align children from bottom to top. If wrapping is enabled, then the next line will start to the right
of the first item on the bottom of the container.
row-reverse Align children from right to left. If wrapping is enabled, then the next line will start under the first
item on the right of the container.
Layout Direction
Layout direction specifies the direction in which children and text in a
hierarchy should be laid out. Layout direction also affects what edge start and
end refer to.
By default, React Native lays out with LTR layout direction. In this mode start
refers to left and end refers to right.
LTR (default value) Text and children are laid out from left to right. Margin and
padding applied to the start of an element are applied on the left side.
RTL Text and children are laid out from right to left. Margin and padding applied to
the start of an element are applied on the right side.
Justify Content
justifyContent describes how to align children within the main axis of their container. For
example, you can use this property to center a child horizontally within a container with
flexDirection set to row or vertically within a container with flexDirection set to column.
flex-start(default value) Align children of a container to the start of the container's main axis.
flex-end Align children of a container to the end of the container's main axis.
center Align children of a container in the center of the container's main axis.
space-between Evenly space off children across the container's main axis, distributing the remaining
space between the children.
space-around Evenly space off children across the container's main axis, distributing the remaining
space around the children. Compared to space-between, using space-around will result in space being
distributed to the beginning of the first child and end of the last child.
space-evenly Evenly distribute children within the alignment container along the main axis. The
spacing between each pair of adjacent items, the main-start edge and the first item, and the main-end
edge and the last item, are all exactly the same.
Align Items
alignItems describes how to align children along the cross axis of their container. It is very similar
to justifyContent but instead of applying to the main axis, alignItems applies to the cross axis.
stretch (default value) Stretch children of a container to match the height of the container's cross axis.
flex-start Align children of a container to the start of the container's cross axis.
flex-end Align children of a container to the end of the container's cross axis.
center Align children of a container in the center of the container's cross axis.
baseline Align children of a container along a common baseline. Individual children can be set to be the
reference baseline for their parents.
Align Content
alignContent defines the distribution of lines along the cross-axis. This only has effect
when items are wrapped to multiple lines using flexWrap.
flex-start (default value) Align wrapped lines to the start of the container's cross axis.
flex-end Align wrapped lines to the end of the container's cross axis.
stretch (default value when using Yoga on the web) Stretch wrapped lines to match the height of
the container's cross axis.
center Align wrapped lines in the center of the container's cross axis.
space-between Evenly space wrapped lines across the container's cross axis, distributing the
remaining space between the lines.
space-around Evenly space wrapped lines across the container's cross axis, distributing the
remaining space around the lines. Compared to space-between, using space-around will result in
space being distributed to the beginning of the first line and the end of the last line.
Flex Wrap
The flexWrap property is set on containers and it controls what
happens when children overflow the size of the container along the
main axis. By default, children are forced into a single line (which
can shrink elements). If wrapping is allowed, items are wrapped into
multiple lines along the main axis if needed.
flexGrow describes how any space within a container should be distributed among its children along the main axis. After laying out its
children, a container will distribute any remaining space according to the flex grow values specified by its children.
flexGrow accepts any floating point value >= 0, with 0 being the default value. A container will distribute any remaining space among
its children weighted by the children’s flexGrow values.
flexShrink describes how to shrink children along the main axis in the case in which the total size of the children overflows the size of
the container on the main axis. flexShrink is very similar to flexGrow and can be thought of in the same way if any overflowing size is
considered to be negative remaining space. These two properties also work well together by allowing children to grow and shrink as
needed.
flexShrink accepts any floating point value >= 0, with 0 being the default value (on the web, the default is 1). A container will shrink its
children weighted by the children’s flexShrink values.
Width and Height
The width property specifies the width of an element's content area. Similarly, the height
property specifies the height of an element's content area.
auto (default value) React Native calculates the width/height for the element based on its
content, whether that is other children, text, or an image.
pixels Defines the width/height in absolute pixels. Depending on other styles set on the
component, this may or may not be the final dimension of the node.
percentage Defines the width or height in percentage of its parent's width or height, respectively.
Absolute & Relative Layout
The position type of an element defines how it is positioned within its
parent.
absolute When positioned absolutely, an element doesn't take part in the normal
layout flow. It is instead laid out independent of its siblings. The position is
determined based on the top, right, bottom, and left values.