1 of 22

Intro to React

2 of 22

About me

  • Software Engineer @ Google NYC
  • Google Forms, former Engineering Resident
  • University of Kansas graduate - go Jayhawks!
    • Freelance web developer

3 of 22

Goal

Understand the basics of React, and learn how to define React components.

4 of 22

Roadmap

  • Overview
  • Declarative programming
  • Components
    • Props
    • JSX
    • State
    • Examples
  • Interactive examples
  • Advanced topics
  • Q&A

5 of 22

What is React?

A declarative, component-based JavaScript library for building user interfaces

Source: https://reactjs.org

6 of 22

Declarative

declarative programming is a programming paradigm — a style of building the structure and elements of computer programs—that expresses the logic of a computation without describing its control flow.

How is React “declarative?” Declare how a component should look, and let React do the low-level job of calling the right DOM API methods to actually render it

7 of 22

Declarative vs. Imperative programming

  • What we want” instead of “this is how you should do it”

8 of 22

React

developers

9 of 22

Understanding Components

  • Components split the UI into independent, reusable pieces
  • Think about each piece in isolation
  • Components = JavaScript functions
    • They accept arbitrary inputs (props)
    • They can manage internal state
    • They return React elements describing what should appear on the screen
  • Only requirement for React components: define a render method.

10 of 22

11 of 22

Understanding Props

  • Unchanging, static values that are passed into a component
  • A parent passes props to a child
  • Essentially, the “inputs” to a React component
  • Examples: title, color, a data object, a CSS class

12 of 22

A component with props: Greeter.js

13 of 22

HelloWorld: using the Greeter component

14 of 22

Greeter.js, with JSX!

15 of 22

JSX

  • A syntax extension to Javascript
  • Makes it easier to describe what the UI should look like
  • Allows you to return HTML elements from a React component’s render method

16 of 22

Want to see what JSX compiles to?

https://babeljs.io/repl

Note: you don’t have to use JSX to use React. If you don’t like it, don’t use it!

17 of 22

What is state?

  • A variable/changing value contained in a React component
  • Unlike props, they are NOT read-only
  • Can you think of times when we’d want a React component to have state?
    • Handling user interaction
    • Timers, counters
    • Receiving data from a database or API
  • Only use state when needed!

18 of 22

A component with state: Counter

19 of 22

Props and State recap

  • Props: unchanging/static values from parent components down to children components
  • State: keep track of changing/updating values related to a component
  • Only use state when you need to, things can get complex quickly!

20 of 22

Interactive Examples

21 of 22

Advanced topics

22 of 22

Thank you!

Questions?