React Documents PDF
React Documents PDF
What is React ?
Nav Bar
Courses Profile
Virtual
DOM
Browser
DOM
JavaScript Executable ( JSX )
• Syntax Extension to JavaScript.
• Produces React “elements”.
React without JSX
• Embedding Expression
• Specifying Attributes
• Represent Objects
• Hooks can be easily used • Hooks can be used with different syntax
• No State Variables • Contains State Variables
Props
• Arguments passed into React Components
• React is pretty flexible but it has a single strict rule:
“All React components must act like pure functions with respect to their props.”
• Pure Functions don’t attempt to change their inputs , i.e. they are immutable
Props
State
• Build-in React Object
• Used to contain data or information about component
• State can change over time
• On change of state , the component re-renders
• A state can be modified based on user action or network changes
• this.setState() is used to change the value of the state object
setState()
Props Vs State
State Props
• State is used to store the data • Props are used to pass data and
Use Case of the components that have to event handlers to the children
be rendered to the view components
• State holds the data and can • Props are immutable—once set,
Mutability
change over time props cannot be changed
App App
Users Users
• Controlled Components
• Uncontrolled Components
Form Elements
• The input Tag
• The textarea Tag
• The select Tag
• The file input Tag
Handling Multiple Inputs
• Multiple inputs are handled by using name Attribute
• To access the fields in the event handler use the event.target.name and
event.target.value syntax.
React Router
react-router-dom
react-router-native
React Router
Types of Routers
• BrowserRouter
• HashRouter
• MemoryRouter
• NativeRouter
• StaticRouter
React Router
Data Routers
• createBrowserRouter
• createMemoryRouter
• createHashRouter
Implementing Routing
useState()
[currStateValue,updatefunction]
State Hook
• State describes the condition of the app at a point in time, and UI renders based on
that state
• When something happens in the app:
The UI dispatches an action
The store runs the reducers, and the state is updated based on what occurred
The store notifies the UI that the state has changed
• The UI re-renders based on the new state
Redux
Problem Statement : Create a Custom Increment App using Redux .
Redux Toolkit
• Initialization
Redux Toolkit
• Create a Redux store with configureStore
- configureStore accepts a reducer function as a named argument
- configureStore automatically sets up the store with good default settings
• Provide the Redux store to the React application components
- Put a React-Redux <Provider> component around your <App />
- Pass the Redux store as <Provider store={store}>
Redux Toolkit
• Create a Redux "slice" reducer with createSlice
- Call createSlice with a string name, an initial state, and named reducer functions
- Reducer functions may "mutate" the state using Immer
- Export the generated slice reducer and action creators
• Use the React-Redux useSelector/useDispatch hooks in React components
- Read data from the store with the useSelector hook
- Get the dispatch function with the useDispatch hook, and dispatch actions as needed
Axios
• HTTP Client Library
• Promise Based
• Allows to make requests to a given endpoint.
• Installation :
Sending HTTP Request
• axios() Function
• Configuration Options
- method: The HTTP method through which the
request should be sent in
- url: The server's URL to which the request must
be sent to
- data: The data specified with this option is sent
in the body of the HTTP request in Axios POST
requests, PUT, and PATCH.
Axios Request Methods
• axios.request(config)
• axios.get(url[, config])
• axios.delete(url[, config])
• axios.head(url[, config])
• axios.options(url[, config])
• axios.post(url[, data[, config]])
• axios.put(url[, data[, config]])
• axios.patch(url[, data[, config]])
Axios Response Objects
• data - the payload returned from the server
• status - the HTTP code returned from the server
• statusText - the HTTP status message returned by the server
• headers - headers sent by the server
• config - the original request configuration
• request - the request object
Axios Post Request
axios.post(“url”,Data Objects)
Axios Get Request
axios.
get(‘u
conso rl’).the
le.log. n(func
}) (respo tion(re
nses ) spons
e){
Babel
Babel
• @babel/core
• @babel/preset-react
• @babel/preset-env
• Babel-loader
Webpack
Includes Includes
JavaScript Files
Bundle Files
(in ES6 !) Bundle Transpile