React is a declarative, efficient, and flexible JavaScript library for building user interfaces. It’s ‘V’ in MVC. ReactJS is an open-source, component-based front-end library responsible only for the view layer of the application. It is maintained by
Facebook.
React uses a declarative paradigm that makes it easier to reason about your application and aims to be both efficient and flexible.
Features of React.js: There are unique features are available on React because that it is widely popular.
npx create-react-app myapp
import React, { Component } from 'react' export class Contact extends Component {
render() {
return (
Contact
)
}
}
export default Contact
import React from 'react' const Contact = () => {
return (
Contact
)
}
export default Contact
npm start
mkdir Utils Assets Components Config Layouts Middleware Pages Routes Services Utils
type NUL > sideBar.js
type NUL > Footer.js
type NUL > header.js
npm i react-router-dom
import { BrowserRouter as Router, Routes, Route } from 'react-router-dom';
<Router>
<Routes>
<Route path="/" element={<Home />}/>
<Route path="" element={<Singlemovie />}/>
<Route path="*" element={<Errorpage />}/>
</Routes>
</Router>
import { BrowserRouter as Router, Routes, Route } from 'react-router-dom';
<Router>
<Routes>
<Route path="/" element={<Home />}>
<Route path="about" element={<About />} />
<Route path="contact" element={<Contact />} />
<Route path="*" element={<Errorpage />} />
</Route>
</Routes>
</Router>
add outlet in parent route in above ex: home.js
import { Outlet } from 'react-router-dom'
<Outlet/>
import { useNavigate } from 'react-router-dom'
<button onClick={()=>{navigate(-1);}}>Go back</button>
According to the definition in React Router doc, useParams returns: an object of key/value pairs of URL parameters. Use it to access match.params of the current route
import { useParams } from 'react-router-dom'
const { id }=useParams();
React Context is a way to manage state globally.
It can be used together with the useState Hook to share state between deeply nested components more easily than with useState alone.
Context provides a way to pass data or state through the component tree without having to pass props down manually through each nested component. It is designed to share data that can be considered as global data for
a tree of React components, such as the current authenticated user or theme(e.g. color, paddings, margins, font-sizes).
Context API uses Context. Provider and Context. Consumer Components pass down the data but it is very cumbersome to write the long functional code to use this Context API. So useContext hook helps to make the code
more readable, less verbose and removes the need to introduce Consumer Component. The useContext hook is the new addition in React 16.8.
import React, { useContext } from "react";
For Create context value
const AppContext = React.createContext();
For set provider value
return <AppContext.Provider value="soham">{children}</AppContext.Provider>
For access value
const auth = useContext(AuthContext);
add appprovider in index.js
<AppProvider> </AppProvider>
function Avatar(props) {
let person = props.person;
let size = props.size;
// ...
}
function Avatar({ person, size }) {
// ...
}
const [state, setState] = useState(initialState)
const [statebtn, setbtn] = useState("Enable dark mode");
const [mode, setMode] = useState({
color: "black",
backcolor: "white",
});
const toogle = () => {
if (mode.color === "black") {
setMode({
color: "white",
backgroundColor: "black",
});
setbtn("Enable light mode");
} else {
setMode({
color: "black",
backgroundColor: "white",
});
setbtn("Enable dark mode");
}
};