React Native Learning
React Native Learning
2. Hello World
In accordance with the ancient traditions of our people, we must first build an app
that does nothing except say "Hello, world!". Here it is:
If you are feeling curious, you can play around with sample code directly in the
web simulators. You can also paste it into your App.js file to create a real app on
your local machine.
The Text component allows us to render a text, while the View component renders
a container. This container has several styles applied, let's analyze what each
one is doing.
The first style that we find is flex: 1, the flex prop will define how your items are
going to "fill" over the available space along your main axis. Since we only have
one container, it will take all the available space of the parent component. In this
case, it is the only component, so it will take all the available screen space.
Some of the things in here might not look like JavaScript to you. Don't panic. This
is the future.
4. Components
So this code is defining HelloWorldApp, a new Component. When you're building a
React Native app, you'll be making new components a lot. Anything you see on
the screen is some sort of component.
5. Props
Most components can be customized when they are created, with different
parameters. These creation parameters are called props.
Your own components can also use props. This lets you make a single
component that is used in many different places in your app, with slightly different
properties in each place. Refer to props.YOUR_PROP_NAME in your functional
components or this.props.YOUR_PROP_NAME in your class components. Here's an
example:
TypeScript
JavaScript
Using name as a prop lets us customize the Greeting component, so we can reuse
that component for each of our greetings. This example also uses
the Greeting component in JSX. The power to do this is what makes React so
cool.
The other new thing going on here is the View component. A View is useful as a
container for other components, to help control style and layout.
With props and the basic Text, Image, and View components, you can build a
wide variety of static screens. To learn how to make your app change over time,
you need to learn about State.
6. State
Unlike props that are read-only and should not be modified, the state allows
React components to change their output over time in response to user actions,
network responses and anything else.
In a React component, the props are the variables that we pass from a parent
component to a child component. Similarly, the state are also variables, with the
difference that they are not passed as parameters, but rather that the component
initializes and manages them internally.
8. Are there differences between React and React Native to handle the
state?
tsx
// ReactJS Counter Example using Hooks!
return (
<div className="container">
<p>You clicked {count} times</p>
<button
onClick={() => setCount(count + 1)}>
Click me!
</button>
</div>
);
};
// CSS
.container {
display: flex;
justify-content: center;
align-items: center;
}
tsx
// React Native Counter Example using Hooks!
return (
<View style={styles.container}>
<Text>You clicked {count} times</Text>
<Button
onPress={() => setCount(count + 1)}
title="Click me!"
/>
</View>
);
};
In the following example we will show the same above counter example using
clas