Coding Notes

React

By Rae

React

  • State
  • Props
  • APIs
  • Hooks
  • JSX

Notes

  • You can use react by bringing react, react-dm, and babel, via CDN but that's not idea.
  • React renders things.
  • React automatically manipulates the DOM when there is a change.
  • Library VS Framework
    • A library is a collection of reusable code. It has no/few boundaries.
    • React is more of a framework.
    • A framework provides structure, a predetermined architecture, a specific pattern for development. It has boundaries you habe to work within.
    • History of Frontend Libraries/Frameworks: 2006: JQuery, 2010: Angualar JS, Ember, Backbone.js, 2013: React, Angualr, vue.js, 2016: Svelte
  • Why React?
    • highest Demand
    • Largest Ecosystem
    • Less magic (easy to understand, as it is js)
    • Composable/Declarative
    • Active development
  • For backend, nodejs is teh best.
  • You need to know JS before you learn React.
  • When NOT to use a framework
    • Small project
    • Network load concerns
    • Learning curve
    • Maintenanceconcerns
    • Incompatability
  • Why use a web framework like React?
    • It's composable
    • It's declarative
  • JSX = JavaScript + HTML
  • React 17
    • const variable = 'string' const element = ( <header> <h1>{variable}'s a string</h1> </header>. ) const root = document.getElementById('root') ReactDOM.render(element, root)
  • React 18
    • const root = ReactDOM.createRoot(document.getElementById('root')) root.render(element)
  • To deploy to netlify just drag and drop the project to app.netlify.com/drop
  • A11y
    • alt
    • Use rem for font-size
    • Use headings for semantic meaning
  • Custom Components
    • function Component() { return <ol> <li>test</li> } ReactDOM.render(<Component />, document.getElementById('root'))
    • Composing a component of other components: //////
    • > npm create vite@latest to build a react app
    • To add style to an element
      • className="nav-items"
    • <script src="index.pack.js"></script>
    • import React from "react" import Navbar from "./components/Navbar" import Main from "./components/Main" React doesn't have to be imported to main or navbar.
    • Send props to a component to make it more reusable
    • <Header title="Great News"> export default function Header(props {return ( <h1>{props.title}</h1>)})
    • Mapping components
    • Conditional Rendering
    • Spread Objects as props
    • When returning JSX there needs to be a parent element wrapping everyting.
    • Event Handlers <button onClick={handleCLick}>
    • A component uses the props sent to it from it's parent without changing it.
    • A component mahy have it's own data, AKA state. State is ata taht can be changed and the componeent gets re-rendered as a result of state change.
    • Maintain a state with a hook: const [count, set count] = React.useState(0) function addd(){ setCount(prevCount => prevCount +1) }
    • Complex State Arrays
      • Arrays
      • Obj
    • A parent can send a copy of the state to the child as a rpop
    • The child cannot change the props
    • In order for the child to change the state in the parent the parent sends a function to the child as props. That function wehnm called from the child, can change the stae in the parent.
    • Styling in React
      • Const styles = { backgroundColor: 'black' }
    • You don't need derived state. Keep state in the parent component.
    • Conditionaal rendering with the ternary opperator
    • add label htmlFor="" for input
    • Main React Tasks
      • Render the UI
      • Kepp track of state
      • Re-render the UI when state changes
    • React wont handle on its own side effects
      • localStorage
      • API/DB interactionsw
      • Subscritions (websockets)
    • render() ---???????
  • JS for React
    • Loops
      • for (let i = 0; i < 5; i++) { console.log(i) } logs 0 1 2 3 4
      • let i = 0 while(i < 5) { console.log(i) i++ } logs 0 1 2 3 4
      • There are other loops, these are just the core ones.
    • Function
      • Newer way to write functions const sum = function(a, b) { return 1 + b } console.log(sum(1, 2)) logs 3
      • A function is a way to reuse code
      • Older way of writing functions console.log(sum(1, 2)) function sum(a, b) { return(a + b) } logs 3. This function is hoisted.
    • Arrow Functions
      • Anonymous functions.
      • const sum = (a, b) => { return a + b } console.log(sum(1, 2)) logs 3
    • Arrays
      • A list of things.
      • const numbers = [1, 2, 3] for (let i = 0; i < numbers.length; i++) { console.log(numbers[i]) }
    • Array Functions: map
      • const numbers = [1, 2, 3] console.log(numbers.map(number => number * 10)) logs 10 20 30
    • Objects
      • const person = { name: 'John', age: 65 } console.log(person['name']) logs John
    • Destructuring
      • const person = { name: 'John', age: 65 } function showDetails ({name, age}) { console.log(name) console.log(age) } showDetails(person) logs John 65
    • Spread Operators
      • const person = { name: 'Tim', age: 65 } const detailedPerson = { ...person, salary: 60000 } function showDetails({name, age, salary}) { console.log(name) console.log(age) console.log(salary) } showDetails(detailedPerson) logs Tim 65 60000
    • Rest Operators
      • const person = { name: 'Tim', age: 65 } const detailedPerson = { ...person, salary: 60000 } function showDetails({name, ...details}) { console.log(name), console.log(detail) } logs John {age: 65, salary: 60000}
    • Short Circuiting
      • const showDetails = true if (showDetails && console.log('See the Details')) { } logs See the Details
      • true && true is true, true && false is false, false && true is false, and false && false is false.
      • const showDetails = true if (showDetails || console.log('See the Details')) { } logs nothing
      • const showDetails = false if (showDetails || console.log('See the Details')) { } logs See the Details
      • true || true is true, true || false is true, false || true is true, and false || false is false.
    • Ternary Operator
      • const showDetails = false showDetails? console.log('Show the Details') : console.log('Show none') logs Show none
      • const showDetails = true showDetails? console.log('Show the Details') : console.log('Show none') logs Show the Details
    • Set up a doc
      • Add ract scripts (react and react dom).
      • Add babel.
      • Add text/babel to modal.
      • Add ReactDom.render() to js file. render will allow you to render something that looks like html.

Resources

How to add react to your project
CDN Links for adding react 18
Install Babel

Definitions

Questions

Opinions