Skip to content

React Native Part 5 - CRUD with Static Data

Published: at 09:09 AM

React Native Basics (Part 5): Components, Props, and State

Introduction

React Native allows you to build mobile apps using components, props, and state. Let’s explore these basic concepts to kickstart your journey.

1. Basic Components in React Native

React Native provides several basic components to create the UI of your mobile app. Here are a few essential ones:

1.1. View

Example:

import React from "react";
import { View, StyleSheet } from "react-native";

export default function App() {
  return (
    <View style={styles.container}>
      <View style={styles.box} />
    </View>
  );
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    justifyContent: "center",
    alignItems: "center",
  },
  box: {
    width: 100,
    height: 100,
    backgroundColor: "blue",
  },
});

1.2. Text

Example:

import React from "react";
import { Text, View, StyleSheet } from "react-native";

export default function App() {
  return (
    <View style={styles.container}>
      <Text>Hello, World!</Text>
    </View>
  );
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    justifyContent: "center",
    alignItems: "center",
  },
});

1.3. Button

Example:

import React from "react";
import { Button, View, StyleSheet } from "react-native";

export default function App() {
  return (
    <View style={styles.container}>
      <Button title="Press Me" onPress={() => alert("Button Pressed!")} />
    </View>
  );
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    justifyContent: "center",
    alignItems: "center",
  },
});

1.4. TextInput

Example:

import React, { useState } from "react";
import { TextInput, View, StyleSheet, Text } from "react-native";

export default function App() {
  const [text, setText] = useState("");

  return (
    <View style={styles.container}>
      <TextInput
        style={styles.input}
        placeholder="Type here..."
        onChangeText={input => setText(input)}
      />
      <Text>You typed: {text}</Text>
    </View>
  );
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    justifyContent: "center",
    alignItems: "center",
  },
  input: {
    height: 40,
    borderColor: "gray",
    borderWidth: 1,
    width: 200,
    marginBottom: 10,
    paddingHorizontal: 5,
  },
});

2. Understanding Props

Props are how components talk to each other. They are like function arguments, providing a way to pass data from a parent component to a child component.

Example:

import React from "react";
import { Text, View, StyleSheet } from "react-native";

function Greeting(props) {
  return <Text>Hello, {props.name}!</Text>;
}

export default function App() {
  return (
    <View style={styles.container}>
      <Greeting name="Alice" />
      <Greeting name="Bob" />
    </View>
  );
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    justifyContent: "center",
    alignItems: "center",
  },
});

Code Explanation

3. Using State

State is a way to store and manage data in a component. When the state changes, the component re-renders to reflect the new data.

Example:

import React, { useState } from "react";
import { Text, View, Button, StyleSheet } from "react-native";

export default function App() {
  const [count, setCount] = useState(0);

  return (
    <View style={styles.container}>
      <Text>You clicked {count} times</Text>
      <Button title="Click Me" onPress={() => setCount(count + 1)} />
    </View>
  );
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    justifyContent: "center",
    alignItems: "center",
  },
});

Code Explanation

Summary

Next Steps

This guide provides a foundation for understanding how React Native apps are built using components, props, and state. From here, you can start building more interactive and complex mobile applications.


Previous Post
React Native Part 4 - CRUD with Static Data
Next Post
React Native Part 6 - CRUD with Static Data