██████ ███████ █████ ██████ ████████ ██ ███████
██ ██ ██ ██ ██ ██ ██ ██ ██
██████ █████ ███████ ██ ██ ██ ███████
██ ██ ██ ██ ██ ██ ██ ██ ██ ██
██ ██ ███████ ██ ██ ██████ ██ ██ █████ ███████ node.jsNPM 5.2+babelJSX syntaxreact routerFirefox
FROM node:18-alpine
RUN npm install --location=global vim
RUN npm install --location=global npm
RUN npm install --location=global semistandard -ydocker build -t node-alp:1.0 .docker run -d -it --rm -p 3000:3000 -v /home/ralex/code:/code --user node --name node-alp-0 node-alp:1.0docker exec -it --user root node-alp-0 sh
Since React is a library, and not a framework, this means that can be used with other JavaScript libraries.
- Lodash - utility library
- Luxon - date and time library
- Redux - state management library
- Axios - HTTP client
- Jest - testing library
This process will install react, react-dom and react-scripts
React will create react componenets, react-dom is is how add them to the page, and react scripts is how we handle the bundling.
node -vnpx create-react-app my-appcd my-appnpm start- open http://localhost:3000 to view it in the browser.
if download the code from github, you need to install the dependencies with
npm install
Principles - React.js
- Don't touch the DOM
- Build websites like blocks
- Unidirection data flow, only move down
- React.js the UI
package.jsonwhat are the different packages or libraries that the project means in order to work, created bycreate-react-app
npm start load into localhost:3000
npm run build build the project in an optimized way ready for production. React create an optimal build
npm run eject don't eject until you find a good reason for that (actually i don't know one)
JavaScript Expressions
- whenever use curly braces
{}is a JavaScript expression and can be used functions
let name = 'John'
{name.toLowerCase()}useState is a hook that allows you to add state to functional components
import React, { useState } from 'react'
const [name, setName] = useState('John')nameis the statesetNameis the function that allows you to change the stateuseStateis the hookuseState('John')is the initial value of the state
useEffect is a hook that allows you to add side effects to functional components
import React, { useState, useEffect } from 'react'
useEffect(() => {
console.log('render')
}, [name])useEffectis the hookuseEffect(() => { console.log('render') }, [name])is the function that will be executed when the component is rendered[name]is the dependency array, if the statenamechanges, the function will be executed
- if the dependency array is empty, the function will be executed only once when the component is rendered
- if the dependency array is not empty, the function will be executed when the component is rendered and when the state in the dependency array changes
- if more than one state is in the dependency array, the function will be executed when any of the states changes
reducer is a hook that allows you to add state to functional components
import React, { useReducer } from 'react'
const [state, dispatch] = useReducer(reducer, initialState)stateis the statedispatchis the function that allows you to change the stateuseReduceris the hookuseReducer(reducer, initialState)is the reducer function and the initial value of the state
useRef is a hook that allows you to add state to functional components
Is used to get the value of an input
import React, { useRef } from 'react'
const inputRef = useRef()inputRefis the stateuseRefis the hookuseRef()is the initial value of the state
React Router is a collection of navigational components that compose declaratively with your application. Whether you want to have bookmarkable URLs for your web app or a composable way to navigate in React Native, React Router works wherever React is rendering--so take your pick!
npm install react-router-dom
npm test
Jest is a delightful JavaScript Testing Framework with a focus on simplicity.
- Jest will run all the test in the project with
.test.jsor.spec.jsextension
The React Testing Library is a very light-weight solution for testing React components. It provides light utility functions on top of react-dom and react-dom/test-utils, in a way that encourages better testing practices.
npm install react-icons --saveinstall the react icons library as a dependency- usage
import { ICONNAME } from 'react-icons/bs
< ICONNAME />-
npm install -D tailwindcss postcss autoprefixer -
npm install -D @tailwindcss/forms -
npx tailwindcss init -p
try first run with react-script Create React App Configuration Override
-
npm install @craco/craco -
modify
package.json
"scripts": {
"start": "craco start",
"build": "craco build",
"test": "craco test"
},- create
craco.config.js
module.exports = {
style: {
postcss: {
plugins: [
require('tailwindcss'),
require('autoprefixer'),
],
},
},
}- create
tailwind.config.js
module.exports = {
purge: ['./src/**/*.{js,jsx,ts,tsx}', './public/index.html'],
darkMode: false, // or 'media' or 'class'
theme: {
extend: {},
},
variants: {
extend: {},
},
plugins: [require('@tailwindcss/forms')],
}- modify the
index.css
@tailwind base;
@tailwind components;
@tailwind utilities;postcss plugins not found
find node_modules/ -name 'postcss-flexbugs-fixes'npm install postcss-flexbugs-fixes postcss-normalize postcss-preset-env