Started learning on 19th December 2021 from this.
- React is a JavaScript library for building user interfaces.
- Developed by Facebook in 2011
- Some of React's competitors: Angular, Vue, Svelte
- React is all about components.
- Components are independent chunks of user interface.
- Components can be as small as one HTML element.
- The benefit of the components is that you can bunch of independent, isolated and most importantly reusable user-interfaces.
- In the app, you can have as many components as you want.
- All apps will have at least one component -
Rootcomponent.
- Comfortable with React
- Applying theory
- Build your projects
- Dev Environment
- Tutorial
- Projects
- Redux
- HTML
- CSS
- JavaScript
[ES6] - Coding Addict - Javascript Nuggets
- NodeJS
node --version, minimum 5.2.0 npx- Browser = Chrome
- Text Editor = Visual Studio Code
- React Developer Tools
pwd- Present Working Directoryls- List of files present in a directorymkdir- Creates a directorycd- Change directorycd ..- Navigate one level upclear- Clears out the consoleArrow UP/Down- Previous commands
npm init- createspackage.json(manifest) file which has a list of dependenciesnpm install <pacakgename> --save- Install pacakge locally and add topackage.jsonnpm install <pacakgename> -g- Install package globally and access it anywhere.npm install <pacakgename> --save-dev- Use it only in development environment
- Technically, you don't need to use
create-react-appto work with React. - You can build the whole setup yourself.
- In the long run,
create-react-appwill save a bunch of time. - Under the hood,
create-react-appusesbabelandwebpack. - A
babelis a JS transpiler that converts the newest JS into the good old JS. - It will kind of ensure that our app runs on older browsers as well.
- Webpack does a lot of other things as well.
- Essentially, webpack works as a module bundler.
- The main features of webpack would be bundling resources, watching for changes and running babel transpiler.
- To create a new React app, use
npx create-react-app <appname>
node_modules- contains all the dependencies needed for the projectpublic- contains the files that will be rendered on the browsersrc- contains theApp.jsand other components?
- Removed the default boiler plate stuff from
src - Creating an empty
index.js. - Component names should be capitalized.
- In the
index.js, I will add the following content.
import React from "react";
import ReactDom from "react-dom";
function Greeting() {
return <h4>This is Kamal and this is my first component</h4>;
}
ReactDom.render(<Greeting />, document.getElementById("root"));- All components must be closed. For example, Either like
<Greeting/>or like this<Greeting></Greeting>. - Stateless functional components will always JSX
- Return single element
- div / section / article / fragment
- fragment looks like this:
<> ... </>,...represent content. - use camelCase for html attribute
classNameinstead ofclass- close every element
- formatting
- You can have only one default export per file.
- useState
- useEffect
- Conditional Rendering
- Forms
- useRef
- useReducer
- Prop Drilling
- Context API / useContex
- hooks will start with
use - components that will be used in these hooks must start with a capital letter and should be in capital case.
- Hooks can be called only inside of the body of a function component.
- You cannot call the hook conditionally. (i.e. based on some condition)
- runs after every re-render
- cleanup function
- second parameter
- If you add a empty list
[]as the second argument to theuseEffecthook, then that is executed only in the initial render. - there is no limit on the number of
useEffecthooks in a given file. - Clean up functions will be added as a return statement in this hook.
- useEffect cannot be asynchronous.
- i.e. you can not use
asyncandawaithere.
- useRef works a lot like useState
- of course there are some differences
- as for the similiarties:
- it also preserves the values in between the render
- unlike useState, useRef does not trigger re-render.
- it targets, DOM nodes/ elements
- it is used when you have a more complicated setup
- for simple to do app, it wont be needed, but for complex apsps, then yes, it is recommended to use.
const [state, dispatch] = useReducer(reducer)- reducer is a function
const reducer = (state, action) => { return state;}- always return some kind of state in the reducer function.