Upgrade to Pro — share decks privately, control downloads, hide ads and more …

DevCoach 178: React | Teknik React Menghidupkan...

Nad
December 01, 2024
42

DevCoach 178: React | Teknik React Menghidupkan UI dengan State

Nad

December 01, 2024
Tweet

More Decks by Nad

Transcript

  1. Todayʼs Topics • Class, The Legacy Component • Event Handling

    • React Hooks • Pengenalan React DevTools React
  2. createElement, The React Legacy React import React from 'react'; const

    element = React.createElement( 'h1', { id: 'my-title', className: 'text-red', }, 'Biodata Perusahaan', );
  3. HTML in JS const heading = React.createElement('h1', null, 'React'); const

    strong = React.createElement('strong', null, 'best tool'); const paragraph = React.createElement('p', null, ['The ', strong, ' for building UI']) const divContainer = React.createElement('div', { className: 'container' }, [heading, paragraph]); const divContainer = ( <div className="container"> <h1>React</h1> <p>The <strong>best tool</strong> for building UI</p> </div> ); HTML in JS Version
  4. Component State klik! Interaksi pengguna akan memperbarui state State merupakan

    data di dalam component yang bila nilainya berubah component akan me-render ulang. Nilai state akan berubah ketika ada interaksi dari pengguna.
  5. Implementasi Component State class Counter extends React.Component { constructor(props) {

    super(props); // Inisialisasi state this.state = { count: 0 }; } onIncrease() { this.setState((prevState) => { return { count: prevState.count + 1 } }); } render() { return ( <div> <h1>{this.state.count}</h1> <button onClick={() => this.onIncrease()}> Increase </button> </div> ); } }