Tryna learn react bcz atoms in my life don't.
- the difference between ES5 and ES6 versions (constructor/function method)
- what is
babel.js
,webpack
,ESlint
- added
node-modules
, presets and plugins for local development environment (not using official React Environment by Facebook as of now) - converted a file
main.js
from ES5 to ES6 asoutput.js
using babel transpiler - couldn't update
package.json
tho :P
- how to set up my first react component using CDN,
react.production.min.js
,react-dom.production.min.js
andbabel standalone
on my local host - syntax of JSX, refers to JS + XML
- working of
state
andrender()
functions - inside
state
: define properties of the UI, similar to a class object properties in a basic JS class - inside
render()
: usingdiv
to wrap whole JS code, JS code used for dynamic input like{ i am a dyanamic input }
- rendered the react DOM component by wrapping it in a
div
- using
setState
to handle state in DOM events - learned the scope of
this
reference in the class - added events to the component using
arrow functions
and event variable - monitored the working of code using React Developer Tools add-on in Firefox 70
- [ https://addons.mozilla.org/en-US/firefox/addon/react-devtools/?src=search ]
- [ https://www.youtube.com/playlist?list=PL4cUxeGkcC9ij8CfkAY2RAGb-tmkNwQHG ]
- [ https://reactjs.org/docs/cdn-links.html ]
- introduction to forms in React, defining
input
types, buttons and responsiveness to the UI - preventing default action for a DOM event using
preventDefault()
function in JS, hence defining our own actions or response to the console - defining property/attribute to the whole form instead of the single button i.e. responding for both [ if a user clicks on
submit
button ] and [ user pressesenter
key ]
- using
create-react-app
to get the official React template and development server, up and running - it helps keep the code modular (nested components), has ES6 features by default, creates a development server and uses build tools to create optimized code
- started building
my-lovely-react-app-1
as a Single Page App; rendersindex.html
page only, other pages are loaded as components references from the initial page - removed App.css, logo.svg, App.test.js and their references from src folder to maintain simplicity
- created first component
Ninjas.js
, hard-coded theelements like Name - added
export default
to add theimport reference
to theApp.js
via RegEx addressing, nested using self closing HTML tag<Ninjas />
- used
props
to define cross-component reference, usingthis.props.<attribute>
in - used
const
to define an array of above mentioned properties to be used locally in the specified component asconst { <x1>, <x2>, <x3> } = this.props;
im backkkk after a long time, finally yes
- used props instead of hard-coding helps to have a reusable set of data; it allows receiving of all the data just using one reference prop in each component; just like
App.js
defines data to be sent andNinjas.js
(component) receives all instances of that data by using props only once - outputted many instances of a same attributes in following ways:
- either write each attribute set singly ( just like for each object in a class )
- or make a list ( that will automatically output the data serially )
- in
App
, usedstate
method to make an array of prop-attributes to store the data instances serially; referred to these attributes by their collective name (ninjas
) inrender
method to display on the DOM - in
Ninjas
, destructured props usingconst
keyword to reference props-attributes sequentially in the component - used
map
method to output the prop-list in a sequential manner- divided data into small html chunks of prop-attributes for each instance
- mapped them like objects using arrow function
- added a sequence identifier
key
usingid
attribute(any unique attribute)
- [ https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment ]
container component
: contains state, contains lifecycle hooks, not concerned with UI, use classes to create, methods like render are available;ui component
: doesn't contain state, receive data from props, only concerned with UI, use functions to create- for eg: for a form, use
container
to store data, for nav-bar, useUI
to show styles and represent data for user - make a functional/ UI component (
Ninjas.js
) usingconst
keyword andarrow function
, passingprops
in that function and removing any state/render methods,this
keyword (because no class is there) used previously - use conditional outputting to filter the props instances : if / ternary
- improve the code formatting by wrapping dynamic data under return method only
- designed a React form to catch data entered by user and send it to the main
App
Component - added form labels like name, age, belt in html using
htmlFor
property and associated every target.value to their respective state labels i.e. input toname
label gets stored in 'name' state - this makes using
id
property as a key helpful, to uniquely identify attribute to which the target.value must be saved/assigned onSubmit
function associated withform
label(and notbutton
label) helps to catch all the prompts i.e. button click AND enter key as a marker for completion of form/submission of data- in
handleChange
function, theset.State
method sets the [ ] to the label value entered by the user, much like traversing thestate
array to find the matchingid
property i.e. [ 'name ] = name - on submitting, the
handleSubmit
function prevents the default action of refreshing the page and outputs the state just entered by the user to the console - for the incoming data from user, set
id
using random function from Math library for quick entry(or you can enter id manually/ or the id may be provided by the user itself) - do not set the state of incoming data outside set.State function by using :
this.ninjas.push(ninja)
statement(considered a bad practice, it overwrites the existing data) - set state of incoming data inside the
set.State
function using local variablelet
which helps to create a new array based on attributes of the output ...
(spread operator) spreads thestate
array out into individual attributes and puts them into new array calledninjas
( made usinglet
) attribute by attribute, which makes a carbon copy of theninjas
array from state and then adds an instance of the newninja
entered by the user- copy the new
ninjas
intoninjas
of state (replacing it) - pass
deleteninja
function as a prop to theninjas.js
and use it to setonClick
property for the delete button for separate ninjas - cannot pass function with argument
ninja.id
in the property because it resembles the normal function call, must turn it into object by using anonymous arrow function; otherwise function will be automatically called and we only what it to be called on prompt i.e. by clicking the button - in
deleteninja
function, passid
as a prop in render method; use filter method (as it is non-destructive) to filter the array directly and only output the prop-instances which return true for the given condition (here:id
of ninja must be equal to theid
of the clicked button) - the virtual DOM compares the current and new changes received in the ninjas.js jsx template as props and wherever it finds the difference, it updates to the DOM/browser
- use CSS in React: either define each css file (with same name as each file) or use index.css for all files
- Components in React have lifecycles; lifecycle methods: to have data at any level of app; Phases of React App: mounting, updating, unmounting
- Methods:
- constructor(): not necessary to call, sets state directly inside constructor
- getDerivedStateFromProps(): enables component to update internal state for changes in state, triggers on first render; when we receive updates props, compares props to current state
- shouldComponentUpdate: receives next props and next state, compare next with prev, return false if don't want to change; alternative: use pure components
- render(): takes jsx code and renders it to the DOM, only required method
- getSnapshotBeforeUpdate(): read access to the DOM before change is committed, get current values and return that value in final update hook
- componentDidMount(): fires on first render i.e. when component mounts, good place to get data from external database like Firebase
- componentDidUpdate(): get external data, don't update data inside this or you get a infinite loop
- componentDidUnmount(): fires when component is unmounted
- creating new todo app :DDD -- added components, addition and deletion of todo's, added click events
[ https://projects.wojtekmaj.pl/react-lifecycle-methods-diagram/ ] [ https://materializecss.com/getting-started.html ]
- Todo app completed! UwU [ will add a whole app description in app's readme that will include features and all]
- how webpages load? : Browser makes an initial request to the server to load
/home
, server responds withindex.html
- React is built to load single page applications : they help to decrease load time(no refreshing)
- how do we load multiple page applications then?! - the app.js components sits as a base and all the components like home.js, about.js load over it - when request and the req doesnt go to server but is intercepted in between by the react router and - stops request to going to server and injects the component we need over it(app.js) i.e. /about
- starting new web-app MyPokeTimes! eee [ will add a whole app description in app's readme that will include features and all]
- BrowserRouter uses
react-router-dom
module - it helps to load several components using Route ( found in
react-router
) path in the app.js component
[ - CDN link for materializecss at: https://cdnjs.cloudflare.com/ajax/libs/materialize/1.0.0-rc.2/css/materialize.min.css ]
- use NavLink and Link from
react-router-dom
to stop sending requests to server; each page is not loaded again and again, but only the component is loaded - different from anchor tag [ ] in that way(doesn't reload)
- using NavLink helps to show current/active state of the loaded page other than basic a=href (as is shown using Link)in console --> this helps to add custom design using the available parameters to the current/active page
- Router attaches
props
info automatically to the components like "/contact"(which use NavLink); if we pass theprops
in Contact.js class component , the Router will automatically load the information to the console, eg:history (and other parameters) - BrowserRouter attaches property to the Route components only and not any other component like Navbar
- redirect pages using
setTimeout(path, state)
function that takes two argument, 1: change of props via anonymous function, 2: time(in ms)
- higher order components help to wrap the components with superpowers
withRouter
--> (a higher order component which gives access to various properties)- return
withRouter(navbar)
to wrap the component and apply the properties to that component --> supercharged component
Rainbow.js
helps to randomize color selection from array and assign a random colour to theAbout.js
componentRainbow.js
is a manually written HOC- it returns props and changed color of the class wrapped (in the original about.js, it gives about.js a new superpower)
- in about.js, export is wrapped in Rainbow
- using
routing
components --> programmable redirects
[ - https://reactjs.org/docs/higher-order-components.html ]
- rest api --> show dyanamic database to our users
- jsonplaceholder.com --> gives api endpoints to use in our apps
- http request library :
axios
[ install asnpm install axios
] - helps to go out and fetch data from an exernal Resources (could use
fetch
instead of axios ) - use lifecycle hook
componnetdidmount
(refer axios parameters inside it) --> makeHome.js
a fucntional component to use lifecycle hook axios.get
is asynchronous--> takes some time to get database- return a
promise
which waits for a signal(of above request completion) and then loads the relevant stuff .then
method operates when you get a promise i.e. the job is complete --> is used insideaxios.get
response
from theaxios
is then sliced usingslice
--> show only relevant stuffdata
property insideresponse
object helps to add postspostlist
cycles through the list of posts (if they are present--> checks length) from state and puts them in cards seperately- part of URL that can change -->
route parameters
- usually see them on URL for indivisual records eg: mysite.com/users/yoshi2k1 mycookingsite.com/recipes/2345
- in
app.js
we are expecting a route path with dyanamic id like :/:path_id
which will connect to the componentPost.js
- in
post.js
, we identify theroute param (here:id)
so that we can output the associated post to that id(route param) - use
props.match.params.<name of param>
to match thatpost_id
incomponentDidMount
lifecycle hook - to output the certain post page for each post dyanamically, get the
post_id
dyanamically and associate the post with it - the
link
tag fromreact-router-dom
helps to link thespan
title inhome.js
to the individual post page dyanamically - use
+
inget
method ofaxios
to fetch the exact id match from the posts - use the same logic as used in
home.js
to build the post page i.e. length and all