Professional Documents
Culture Documents
React Fundamentals
React Fundamentals
React Fundamentals
REACT DEFINITION
NODE VERSION
REACT DEVELOPER TOOLS - CHROME EXT.
CREATE REACT APP
OPEN VSCODE TERMINAL SHORTCUT
DRAG & DROP YOUR REACT PROJECT FOLDER INTO THE TERMINAL
INSTALL CREATE REACT APP
FUNDAMENTALS
Folder Structure
REMOVE CREATE-REACT APPS BOILERPLATE
APP.JS
APP.CSS / INDEX.CSS
INDEX.JS
TOGGLE VSCODE SIDEBAR
REACT COMPONENTS
IMPORTS
ES7+ React/Redux/React-Native snippets
rafce (Arrow FN - w/ Export) / rfce (Regular FN - w/ Export)
JSX RULES
<React.Fragment> - RETURNING MULTIPLE ELs - return <>...rest of the return</>;
NESTING COMPONENTS
IMAGES - src folder = optimized.
JSX - CSS (inline styles)
JSX - JS = { } ({ } - Means Inserting JS)
PROPS
Props - Somewhat Dynamic Setup
PROPS DATA FLOW (= From Where Component is Injected to Actual Component
FN)
CHILDREN PROP - useful when an Instance of a Component (= Where its Injected)
needs to Render Diff. HTML than Other Instances
In Actual Component > const { img, title, author, children } = props;
…
{children}
</article>
REACT DEFINITION
*”A JS Library for building UIs”
NODE VERSION
*Always use the LTS Ver. (= Stable) - bc it Takes a While for Other Dependencies, etc to
Catch Up to the Latest (Bleeding Edge) Ver.
https://create-react-app.dev/docs/getting-started/
*These days you don’t Install “Create React App” & then Create Instance of React -
instead you use the NPX CMD (Node Package Executor)
NPX is a package executer, and it is used to execute javascript packages directly,
without installing them.
NPM BUILD
*Bundles your APP into Static Files for Production
> npm run build
CTRL/C
*To Stop the Server > npm start i (To restart it)
FUNDAMENTALS
Folder Structure
node_modules Contains all dependencies required by the app. There are heaps in
here bc - eg. Our Depenedencies have Dependencies.
Main dependencies also listed in package.json
public Contains static assets including index.html (page template)
Index.html, title, fonts, css, favicon
*In the index.html there is a <div id="root"> - our entire app gets INJECTED here
From our Working DIR (‘src’)
package.json Every Node.js project has a package.json and it contains info about our
project, for example list of dependencies and scripts
*Also called “manifest File”
README The markdown file where you can share more info about the project for
example build instructions and summary
APP.JS
*App.js (= Convention to name it this) - is where all our Components will meet
*Import it in “index.js” > import App from './App';
APP.CSS / INDEX.CSS
*Part of “CREATE-REACT” Boilerplate - they are for Splitting up your CSS - BUT they
compile to the SAME PLACE - so = CONFLICTS can occur
*We REMOVED App.css
INDEX.JS
*This Injects the HTML into index.html
*Our Backroads Project - index.js:
// IMPORTs
import React from 'react';
import ReactDOM from 'react-dom/client';
import './index.css';
REACT COMPONENTS
*Are Created using FNs - so think “FN” when you think of a Component
*Start with capital letter
*Must return JSX (html)
*Close TAGs - either by: <Greeting></Greeting> OR <Greeting />
IMPORTS
*JS Files = DONT Need Extension
// These are Libraries - so DONT Have to Specify the PATH (Only = Library Name)
import React from 'react';
import ReactDOM from 'react-dom/client';
// But Our Own Assets - Specify the PATH
import './index.css';
Typical Component
const Greeting = () => {
return <h2>My First Component</h2>;
};
MY FIRST COMPONENT
*In index.js:
function Greeting() {
return <h2>My First Component</h2>;
}
// The ROOT <div> where ALL JSX gets INJECTED INTO (Render to)
const root = ReactDOM.createRoot(document.getElementById('root'));
root.render(<Greeting />);
JSX RULES
*Must only return single element (one parent element)
*semantics - section/article - his recc (save you hassles later)
*Also = Good for Returning ADJACENT EL’s (Which Normally isnt allowed)
NESTING COMPONENTS
*In React we Nest Components
*We can then Nest Components in those also
function Greeting() {
return (
<div>
<Person />
<Message />
</div>
);
}
// Components to be Nested
const Person = () => <h2>john doe</h2>;
const Message = () => {
return <p>this is my message</p>;
};
IMAGES
*external images (hosted on different server) - just need an url
*local images (public folder) - less performant
*local images (src folder) - better solution for assets, since under the hood they
get optimized.
PROPS
*Props allow you to pass data from one component to another, as parameters.
*The Props object (= an OBJ Received as PARAMs), it is a convention to call it
props
= passed in as key/value pairs
*If the prop exists it will return value, otherwise no value
function BookList() {
return (
<section className='booklist'>
<Book
author={firstBook.author}
title={firstBook.title}
img={firstBook.img}
/>
<Book
author={secondBook.author}
title={secondBook.title}
img={secondBook.img}
/>
</section>
);
}
function BookList() {
return (
<section className='booklist'>
<Book author={myObj.author} title={myObj.title} img={myObj.img} />
</section>
);
}
const Book = (props) => {
console.log(props);
return (
<article className='book'>
<img src={props.img} alt={props.title} />
<h2>{props.title}</h2>
<h4>{props.author} </h4>
</article>
);
};
CHILDREN PROP
*This is a SPECIAL NAMED PROP Called “children” is ALWAYs Passed Inside
PROPs OBJ
*It Renders E/Th you include between the opening and closing tags when
invoking a component.
*A Use case of this is if we want a <p> & <btn> to Appear in 1 “Book” Component but
not in the Other one - by Embedding {children} in the “Book” Component, that means
Even though the Same “Book” Component Template is being used for Both “Books” - it
will Render Whats Between the Opening & Closing TAGs for ea. Instance of “Book”
*So by Putting the Extra JSX Markup in the 1st “Book” Component - bc we embed
{children} in the “Book” Components Templates - what ever is in between the Opening &
Closing TAGs for ea. Given “Book” Component will be Rendered (thus = Different
Content)
const Book = (props) => {
const { img, title, author, children } = props;
Ex:
function BookList() {
return (
<section className='booklist'>
<Book
author={firstBook.author}
title={firstBook.title}
img={firstBook.img}
>
<p>
Lorem ipsum dolor, sit amet consectetur adipisicing elit. Itaque
repudiandae inventore eos qui animi sed iusto alias eius ea sapiente.
</p>
<button>click me</button>
</Book>
<Book
author={secondBook.author}
title={secondBook.title}
img={secondBook.img}
/>
</section>
);
}
function BookList() {
return (
<section className='booklist'>
KEY PROP
*When you Render items in React, React wants to keep track of those
items.
So we need to provide a unique value and set it equal to a key prop.
And typically it's going to be an ID.
- eg. OBJs that you access thru APIs will ea. have an ID
EVENTS
*No need to memorize them (idea for all of them is the same) - look them up on React
DOCs when need to use them
*Most common: onClick, onSubmit, onChange (input change)
*Use camelCase for EVT TYPES - onClick, etc
*Syntax = > <button onClick={handleButtonClick}>click me</button>
(EL’) (EVT) = (HANDLER)
*Typical EVT & HANDLER:
// HANDLER
const handleButtonClick = (e) => {
// FORM Attris
alert(e.target.value);
alert(e.target.name);
};
return (
<section>
<button onClick={handleButtonClick}>click me</button>
</section>
);
};
return (
<section>
<button onClick={() => console.log('hello there')}>click me</button>
</section>
PROP DRILLING
*Prop drilling is the unofficial term for passing data through several nested children
components
*Can ONLY Pass PROPs = DOWN
*Can become a prob. Bc may have to Pass the Data through many Levels to get it to its
Destination - this is why Alternatives like: “Context API”, Redux & Other State Libraries
- bc we NEED the Parentheses to Pass the Param (= “id”) - we need a way to get
Around this prob
- So the 1st SOLUTION was - to Wrap the FN in Another & Pass a Reference to a
FN.
Wrap the “getBook(id)” FN CALL - INSIDE Another FN (that obviously doesnt need
Parentheses (Else = Same Prob again) & Call the Outer FN onClick:
ES6 MODULES