My Blog

My WordPress Blog

My Blog

My WordPress Blog

Nativify

Setting Up Your Environment

Make sure you have Node.js and npm installed. You can create a new React Native project using Expo:

  1. Install Expo CLI if you haven’t already:bashCopy codenpm install -g expo-cli
  2. Create a new project:bashCopy codeexpo init nativify-app cd nativify-app
  3. Choose a blank template when prompted.

Step 2: Create Basic Components

We’ll create two main components: ItemList and AddItem.

1. Create ItemList.js

In the components folder (create it if it doesn’t exist), create a new file called ItemList.js:

jsxCopy code// components/ItemList.js
import React from 'react';
import { View, Text, FlatList, StyleSheet } from 'react-native';

const ItemList = ({ items }) => {
  return (
<FlatList
  data={items}
  keyExtractor={(item, index) => index.toString()}
  renderItem={({ item }) => (
    <View style={styles.itemContainer}>
      <Text style={styles.itemText}>{item}</Text>
    </View>
  )}
/>
); }; const styles = StyleSheet.create({ itemContainer: {
padding: 15,
borderBottomWidth: 1,
borderBottomColor: '#ccc',
}, itemText: {
fontSize: 18,
}, }); export default ItemList;

2. Create AddItem.js

Now create another file called AddItem.js:

jsxCopy code// components/AddItem.js
import React, { useState } from 'react';
import { View, TextInput, Button, StyleSheet } from 'react-native';

const AddItem = ({ onAdd }) => {
  const [inputValue, setInputValue] = useState('');

  const handleSubmit = () => {
if (inputValue) {
  onAdd(inputValue);
  setInputValue('');
}
}; return (
<View style={styles.container}>
  <TextInput
    style={styles.input}
    placeholder="Add new item"
    value={inputValue}
    onChangeText={setInputValue}
  />
  <Button title="Add" onPress={handleSubmit} />
</View>
); }; const styles = StyleSheet.create({ container: {
flexDirection: 'row',
marginVertical: 20,
}, input: {
borderWidth: 1,
borderColor: '#ccc',
flex: 1,
padding: 10,
marginRight: 10,
}, }); export default AddItem;

Step 3: Main Application Component

Now, let’s integrate everything in App.js.

jsxCopy code// App.js
import React, { useState } from 'react';
import { View, Text, StyleSheet } from 'react-native';
import ItemList from './components/ItemList';
import AddItem from './components/AddItem';

const App = () => {
  const [items, setItems] = useState([]);

  const addItem = (item) => {
setItems([...items, item]);
}; return (
<View style={styles.container}>
  <Text style={styles.title}>Nativify Item List</Text>
  <AddItem onAdd={addItem} />
  <ItemList items={items} />
</View>
); }; const styles = StyleSheet.create({ container: {
flex: 1,
padding: 20,
backgroundColor: '#fff',
}, title: {
fontSize: 24,
fontWeight: 'bold',
marginBottom: 20,
}, }); export default App;

Step 4: Run Your Application

Start your application using:

bashCopy codeexpo start

This command will open a new tab in your browser, allowing you to run the app on an emulator or your physical device using the Expo Go app.

Summary

In this tutorial, you created a simple React Native app that allows users to add items to a list. You learned how to manage state and create reusable components in React Native.

You can enhance the application by adding features like:

  • Removing items
  • Editing items
  • Storing data with AsyncStorage
Nativify

Leave a Reply

Your email address will not be published. Required fields are marked *

Scroll to top