Basic React Native Part 22 - React Native Slider

react-native Jan 20, 2020

Pada  Part 21 kita telah belajar dan mengenal WebView jadi jika anda belum mengikuti tutorial Part 21 diharapkan anda mengikuti tutorial Part 21 terlebih dahulu. Pada Tutorial Part 22 ini kita mengenal terlebih dahulu Basic React Native Slider.

Slider adalah komponen react native untuk merubah nilai baik berupa menambah atau mengurangi nilai. kita di sini memberikan contoh dari slider yang akan membantu kalian untuk mengintegrasikan slider di aplikasi kalian.

Mengimplementasikan Pada Project React Native

Buat Project React Native

Pada Tutorial Part 2 kita telah menginstall react-native-cli kemudian kita telah belajar cara membuat project baru react native. Berikut perintah cara untuk membuat project baru react native.

react-native init namafolderproject
Buat Project Baru

Install Aplikasi Debug Ke Handphone Android

Pada Tutorial Part 4 kita telah belajar cara menginstall Aplikasi Debug ke Handphone Android, untuk tata cara yang lebih jelas bisa langsung pelajari lagi pada Tutorial Part 4 terlebih dahulu. Secara singkat perintahnya seperti berikut.

react-native run-android
Install Aplikasi Debug Ke Handphone Android

Buka File App.js Dengan Visual Studio Code

Secara default pada file App.js memiliki code yang cukup dan menghasilkan tampilkan seperti berikut ini

import React from 'react';
import {
  SafeAreaView,
  StyleSheet,
  ScrollView,
  View,
  Text,
  StatusBar,
} from 'react-native';

import {
  Header,
  LearnMoreLinks,
  Colors,
  DebugInstructions,
  ReloadInstructions,
} from 'react-native/Libraries/NewAppScreen';

const App: () => React$Node = () => {
  return (
    <>
      <StatusBar barStyle="dark-content" />
      <SafeAreaView>
        <ScrollView
          contentInsetAdjustmentBehavior="automatic"
          style={styles.scrollView}>
          <Header />
          {global.HermesInternal == null ? null : (
            <View style={styles.engine}>
              <Text style={styles.footer}>Engine: Hermes</Text>
            </View>
          )}
          <View style={styles.body}>
            <View style={styles.sectionContainer}>
              <Text style={styles.sectionTitle}>Step One</Text>
              <Text style={styles.sectionDescription}>
                Edit <Text style={styles.highlight}>App.js</Text> to change this
                screen and then come back to see your edits.
              </Text>
            </View>
            <View style={styles.sectionContainer}>
              <Text style={styles.sectionTitle}>See Your Changes</Text>
              <Text style={styles.sectionDescription}>
                <ReloadInstructions />
              </Text>
            </View>
            <View style={styles.sectionContainer}>
              <Text style={styles.sectionTitle}>Debug</Text>
              <Text style={styles.sectionDescription}>
                <DebugInstructions />
              </Text>
            </View>
            <View style={styles.sectionContainer}>
              <Text style={styles.sectionTitle}>Learn More</Text>
              <Text style={styles.sectionDescription}>
                Read the docs to discover what to do next:
              </Text>
            </View>
            <LearnMoreLinks />
          </View>
        </ScrollView>
      </SafeAreaView>
    </>
  );
};

const styles = StyleSheet.create({
  scrollView: {
    backgroundColor: Colors.lighter,
  },
  engine: {
    position: 'absolute',
    right: 0,
  },
  body: {
    backgroundColor: Colors.white,
  },
  sectionContainer: {
    marginTop: 32,
    paddingHorizontal: 24,
  },
  sectionTitle: {
    fontSize: 24,
    fontWeight: '600',
    color: Colors.black,
  },
  sectionDescription: {
    marginTop: 8,
    fontSize: 18,
    fontWeight: '400',
    color: Colors.dark,
  },
  highlight: {
    fontWeight: '700',
  },
  footer: {
    color: Colors.dark,
    fontSize: 12,
    fontWeight: '600',
    padding: 4,
    paddingRight: 12,
    textAlign: 'right',
  },
});

export default App;
Code Default App.js
Tampilan Default File App.js
Tampilan Default File App.js

Menghapus Code Default App.js

Untuk memudahkan memahami component WebView kita hapus semua code dafault App.js. Setelah semua code default App.js dihapus maka ikuti langkah - langkah berikut ini.

npm install @react-native-community/slider --save
Install Slider
  • Import React
import React, { Component } from 'react'
import react
  • Import Component React Native dan Slider  Untuk Project React Native dari Libaray Slider
import Slider from '@react-native-community/slider';
import { View, Text, StyleSheet } from 'react-native';
Import Component
  • Membuat Class Componet
class App extends Component {
  render() { 
    return (  );
  }
}
 
Membuat Class
  • Buat state, tambahkan code berikut ini diatas rende
class App extends Component {
//Buat State
constructor(props) {
   super(props);
   this.state = {
     sliderValue: 15
   };
 }    
  render() { 
    return (  );
  }
}
  • Kemudian tambahan code berikut ini didalam return.
return (
    //Code return
      <View style={styles.container}>
        <Text style={{color: 'black'}}>Value of slider is : 		             {this.state.sliderValue}</Text>
        <Slider 
          maximumValue={100}
          minimumValue={0}
          minimumTrackTintColor="#307ecc"
          maximumTrackTintColor="#000000"
          step={1} 
          value={this.state.sliderValue}
          onValueChange={(sliderValue) => this.setState({ sliderValue })}
        />
      
      </View>
    );
Tambahkan code pada return
  • Tambahkan Style sebelum export default
//Buat Style
const styles = StyleSheet.create({
 container: {
   flex: 1,
   padding:20,
   justifyContent: 'center',
   backgroundColor: '#ecf0f1',
 }
});
  • Setelah itu export class App.
export default App
export class
  • Berikut Code lengkap nya
import React, { Component } from 'react';
import Slider from '@react-native-community/slider'
import { View, Text, StyleSheet } from 'react-native';
 
 class App extends Component {
//Buat State
     constructor(props) {
   super(props);
   this.state = {
     sliderValue: 15
   };
 }
 
  render() {
    return (
        //Code Return
      <View style={styles.container}>
        <Text style={{color: 'black'}}>Value of slider is :             {this.state.sliderValue}</Text>
        <Slider 
          maximumValue={100}
          minimumValue={0}
          minimumTrackTintColor="#307ecc"
          maximumTrackTintColor="#000000"
          step={1} 
          value={this.state.sliderValue}
          onValueChange={(sliderValue) => this.setState({ sliderValue })}
        />
      
      </View>
    );
  }
}
//Buat Style
const styles = StyleSheet.create({
 container: {
   flex: 1,
   padding:20,
   justifyContent: 'center',
   backgroundColor: '#ecf0f1',
 }
});
export default App
  • Tampilan Slider
Tampilan Webview
Tampilan Slider

Demikianlah pembahasan Tutorial Part 22 ini akan kita lanjutkan pada Tutorial Part 23.

Wandi Pratama

Saya seorang Developer Mobile menggunakan Framework React Native dan saya juga memiliki pengalaman dibidang SYS Admin. Jasa menerima jasa pembuatan aplikasi dan website https://pindahdigital.com