Getting Started
What’s React?
A JavaScript library for building user interfaces
- declarative
- declare the view, React does the rest
- component-based
- helps with encapsulation
- learn once, write anywhere
- mobile/desktop apps with React native
- open source / MIT
Why React?
- pros:
- cons:
- batteries not included
- unopinionated
- only 1-way data binding
Start a New App
- use
create-react-app
to start a new app
npx create-react-app my-awesome-react-app --template typescript
cd my-awesome-react-app
npm start
Hello world!
- React just takes a bit of “HTML” and renders it somewhere in DOM
import React from 'react'
import ReactDOM from 'react-dom'
ReactDOM.render(<p>Hello world!</p>, document.getElementById('root'))
Hello world!
Current Time
- can interpolate values into “HTML”
- “HTML” can be passed around as data
import React from 'react'
import ReactDOM from 'react-dom'
const app = () => <div>the time is {new Date().toLocaleTimeString()}</div>
ReactDOM.render(app(), document.getElementById('root'))
the time is 8:19:52 PM
Clock
- “HTML” can be dynamic
import React from 'react'
import ReactDOM from 'react-dom'
const app = () => <div>the time is {new Date().toLocaleTimeString()}</div>
setInterval(() => {
ReactDOM.render(app(), document.getElementById('root'))
}, 1000)
the time is 8:19:52 PM
this is a simple demo, please don’t update using
setInterval