CC-BY-SA This work is licensed under a Creative Commons
Attribution-ShareAlike 4.0 International License

React Concepts

Andy Balaam
artificialworlds.net

Contents

What is React?

Intro to React

Quick start:

Describe the HTML

Describe the HTML

index.js

import React from 'react'; import ReactDOM from 'react-dom'; import './index.css'; import Films from './Films'; ReactDOM.render( <Films />, document.getElementById('root') );

Describe the HTML

Films.js

// ... import Title from './Title.js'; import Search from './Search.js'; import List from './List.js'; export default function Films() { return ( <div className="Films"> <Title /> <Search /> <List /> </div> ); }

Describe the HTML

Describe the HTML

Describe the HTML

Films.js

// ... import Title from './Title.js'; import Search from './Search.js'; import List from './List.js'; export default function Films() { return ( <div className="Films"> <Title /> <Search /> <List /> </div> ); }

Describe the HTML

Search.js

import React from 'react'; import './Search.css'; export default function Search() { return ( <span className="Search"> Search: <input value="foo" /> </span> ); }

Describe the HTML

Search.css

.Search { margin: 0.4vh 2.7vw; display: flex; flex-direction: row; align-items: center; } .Search input { margin-left: 0.4em; font-size: 1em; flex-grow: 1; }

Describe the HTML

Films.js

// ... import Title from './Title.js'; import Search from './Search.js'; import List from './List.js'; export default function Films() { return ( <div className="Films"> <Title /> <Search /> <List /> </div> ); }

Describe the HTML

List.js

export default function List() { const films = [ {"name": "The Matrix", ...} ]; return ( <div className="List"><table> <thead><tr><th>Film</th><th>Genre... <tbody> ... </tbody> </table></div> ); }

Describe the HTML

List.js

<tbody>{ films.map(f => <tr> <td>{f.name}</td> <td>{f.genre}</td> <td>{f.loveorhate}</td> <td>{f.reasoning}</td> </tr> ) }</tbody>

Describe the HTML

Describe the HTML

Films.js

export default function Films() { return ( <div className="Films"> <Title /> <Search /> <List highlight="odfath"/> </div> ); }

Describe the HTML

List.js

export default function List(props) { ... }

Describe the HTML

List.js

films.map(f => <tr> <td>{highlight(props.highlight, f.name)}</td> <td>{f.genre}</td> <td>{f.loveorhate}</td> <td>{f.reasoning}</td> </tr> )

Describe the HTML

Describe the HTML

Describe the HTML

Holding on to state

Web 1.0:

Holding on to state

Web 1.0:

Holding on to state

Web 2.0:

Holding on to state

Web 2.0:

Holding on to state

Holding on to state

Holding on to state

import React, { useState } from 'react'; export default function Search() { const [ search, setSearch ] = useState(""); const searchChanged = (e) => { setSearch(e.target.value); console.log(`Pretending to search: ${e.target.value}`); }; return ( <span className="Search"> Search: <input value={search} onChange={searchChanged} /> </span> ); }

Holding on to state

... const calcClass = () => { if (search.length > 3) { return "bad"; } else { return ""; } } ... <input value={search} onChange={searchChanged} className={calcClass()} /> ...

Holding on to state

Holding on to state

Holding on to state

Events and lifting

... const searchChanged = (e) => { setSearch(e.target.value); console.log(`Pretending to search: ${e.target.value}`); }; return ( ... <input value={search} onChange={searchChanged} /> ...

Events and lifting

Events and lifting

export default function Search(props) { const [ search, setSearch ] = useState(""); return ( <span className="Search"> Search: <input value={search} onChange={props.searchChanged} /> </span> ); }

Events and lifting

export default function Search(props) { const [ search, setSearch ] = useState(""); return ( <span className="Search"> Search: <input value={props.search} onChange={props.searchChanged} /> </span> ); }

Events and lifting

export default function Films() { const [ search, setSearch ] = useState(""); const searchChanged = (e) => { setSearch(e.target.value); console.log(`Pretending to search: ${e.target.value}`); }; return ( <div className="Films"> <Title /> <Search search={search} searchChanged={searchChanged} /> <List highlight={search}/> </div> ); }

Events and lifting

Events and Lifting

Events and Lifting

Events and Lifting

What is really happening?

What is really happening?

What is really happening?

When you understand that cycle, you will understand the rules e.g.:

What is really happening?

The (create-react-app) build:

More info

Videos

youtube.com/user/ajbalaam peertube.mastodon.host/accounts/andybalaam

Social

@andybalaam on Twitter @andybalaam@mastodon.social

Web

artificialworlds.net artificialworlds.net/blog

Code

github.com/andybalaam gitlab.com/andybalaam