rbi-learning / Today-I-Learned

1 stars 0 forks source link

08/24 Day 20 Lecture Notes #166

Open Limlight86 opened 4 years ago

Limlight86 commented 4 years ago

08/24 Day 20 Lecture Notes

React Intro

const jsxElement = <h1>Hello World</h1>

ReactDOM.render(jsxElement, document.getElementById('root'))
const jsxElement = (
  <div className="container">
    <h1>Hello World</h1>
    <img src="some_url" />
  </div>
)

ReactDOM.render(jsxElement, document.getElementById('root'))
<script src="https://unpkg.com/react@16/umd/react.development.js"></script>
<script src="https://unpkg.com/react-dom@16/umd/react-dom.development.js"></script>
<script src="https://unpkg.com/@babel/standalone/babel.min.js"></script>

React Components

const MyCounter = () => {
  return <h1>Hello World!</h1>
}
const Header = () => <h1>This is the header</h1>

const Content = () => (
  <div>
    <p>Some random content here</p>
  </div>
)

const Footer = () => {
  return <h3>Footer Stuff</h3>
}

const App = () => (
  <div>
    <Header />
    <Content />
    <Footer />
  </div>
)

ReactDOM.render(<App />, document.getElementById('root'))

JSX Expressions

const Example = () => {
  return (
    <div>
      <h1>1 + 1</h1>
      <h2>{1 + 1}</h2>
    </div>
  )
}

React State

const [reader, writer] = useState(defaultState)
const Counter = () => {
  const [currentNumber, setCurrentNumber] = React.useState(0);

  return <h1>I am a counter. My current number is {currentNumber}</h1>
}
const Counter = () => {
  const [currentNumber, setCurrentNumber] = React.useState(0);

  const updateCurrentNumber = () => setCurrentNumber(100);

  return (
    <div>
      <h1>The current number is {currentNumber}</h1>
      <button onClick={updateCurrentNumber}>Change number to 100</button>
    </div>
  )
}

Javascript Stylesheet (JSS)

<div className="burger" style={{ display: 'grid', columnGap: 5 }}>

Parcel

import React from 'react'

Event Listeners / Event Handlers / Forms

const WhopperBar () => {
  const [age, setAge] = React.useState('');

  const handleUserInput = event => {
    const newAge = Number(event.target.value);
    setAge(newAge)
  }

  const handleFormSubmit = event => {
    event.preventDefault();

  }

  return (
    <div>
      <h1>Welcome to the Whopper Bar</h1>
      <form onSubmit={handleFormSubmit}>
        <label>How Old Are you</label>
        <input type="number" min="1" requireds value={age} onChange={handleUserInput} />
    </div>
  )
}

Props

  const Counter = props => {
    return (
      <div>
        <h1>This is the {props.title} counter</h1>
      </div>
    )
  }

  const App = () => (
    <div>
      <Counter title="burger" />
      <Counter title="coffee" />
      <Counter title="chicken sandwich" />
    </div>
  )