-
Recap on Chapter-03 - functional component, react elements
-
Creating food ordering app - Food Villa in this session
-
Writing comments in jsx
-
We don't need ES6, jsx, typescript to work in react
-
Create low level design - Create app layout
header
- logo (Title)
- nav items (right side)
- cart
body
- search bar
- restaurantList
- restaurantCard
- image
- Name
- Rating
- Cuisine
footer
-
Create HeaderComponent
- Add TitleComponent -> it has logo img and a tag for the image.
- div nav-items (ul and li )
-
Create Body and Footer Component
-
Create AppLayout: put header, body and footer within <></>
-
JSX has only one parent
- use React.Fragment:
<React.Fragment> </React.Fragment>
) - its shorthand is like empty tag:
<> </>
- use React.Fragment:
-
but if you want to give attrbutes,
style
in div tag- style = { border : red 1px soild} ; is not possible
- So, wrap inside () inside jsx
-
Config driven UI : Different UI layout for different users - Backend driven config
-
Optional chaining i.e
?.
-
Props (Properties)
- attributes passed through component tag
- similar to arguments passed in function in js
- received as parameters in the function (component in react)
-
: const Component = (props)=>{()} :: func(arguments) : const func = (parameters) => {}
-
Virtual DOM -> representation/copy of DOM with us
-
Purpose : React Reconciliation
- React uses diff Algorithm to diff one tree (actually dom) from another which determines what needs to be updated and only re-renders the diff
- re-render everything if key is not mentioned
-
react fiber
-
never use index as key
const restaurantData = {
imageId: "bdcd233971b7c81bf77e1fa4471280eb",
name: "KFC",
cuisines: ["Burger", "Biryani", "American"],
approxDeliveryTime: "45"
}
const RestrauntCard = () => {
return (
<div className="restaurant-card">
<img src={"https://res.cloudinary.com/swiggy/image/upload/fl_lossy,f_auto,q_auto,w_508,h_320,c_fill/" + restaurantData?.imageId} alt="restaurant image" />
<h2>{restaurantData?.name}</h2>
<h3>{restaurantData?.cuisines.join(", ")}</h3>
<h4>{restaurantData?.approxDeliveryTime} minutes</h4>
</div>
)
}
const AppLayout = () => {
return (
<>
<HeaderComponent />
<RestrauntCard/>
<>
)
}
const restaurantData = {
imageId: "bdcd233971b7c81bf77e1fa4471280eb",
name: "KFC",
cuisines: ["Burger", "Biryani", "American"],
approxDeliveryTime: "45"
}
const RestrauntCard = (props) => {
return (
<div className="restaurant-card">
<img src={"https://res.cloudinary.com/swiggy/image/upload/fl_lossy,f_auto,q_auto,w_508,h_320,c_fill/" + props.restaurant?.imageId} alt="restaurant image" />
<h2>{props.restaurant?.name}</h2>
<h3>{props.restaurant?.cuisines.join(", ")}</h3>
<h4>{props.restaurant?.approxDeliveryTime} minutes</h4>
</div>
)
}
const AppLayout = () => {
return (
<>
<HeaderComponent />
<RestrauntCard restaurant={restaurantData} />
<>
)
}
const restaurantDataArray = [
{
imageId: "bdcd233971b7c81bf77e1fa4471280eb",
name: "KFC",
cuisines: ["Burger", "Biryani", "American"],
approxDeliveryTime: "45"
},
{
imageId: "bdcd233971b7c81bf77e1fa4471280eb",
name: "Burger king",
cuisines: ["Burger", "American"],
approxDeliveryTime: "15"
}
]
const RestrauntCard = (props) => {
return (
<div className="restaurant-card">
<img src={"https://res.cloudinary.com/swiggy/image/upload/fl_lossy,f_auto,q_auto,w_508,h_320,c_fill/" + props.restaurant?.imageId} alt="restaurant image" />
<h2>{props.restaurant?.name}</h2>
<h3>{props.restaurant?.cuisines.join(", ")}</h3>
<h4>{props.restaurant?.approxDeliveryTime} minutes</h4>
</div>
)
}
const AppLayout = () => {
return (
<>
<HeaderComponent />
<RestrauntCard restaurant={restaurantDataArray[0]} />
<RestrauntCard restaurant={restaurantDataArray[1]} />
<>
)
}
- Object Destructuring
(props) => ({restaurant})
const restaurantDataArray = [
{
imageId: "bdcd233971b7c81bf77e1fa4471280eb",
name: "KFC",
cuisines: ["Burger", "Biryani", "American"],
approxDeliveryTime: "45"
},
{
imageId: "bdcd233971b7c81bf77e1fa4471280eb",
name: "Burger king",
cuisines: ["Burger", "American"],
approxDeliveryTime: "15"
}
]
const RestrauntCard = ({restaurant}) => {
return (
<div className="restaurant-card">
<img src={"https://res.cloudinary.com/swiggy/image/upload/fl_lossy,f_auto,q_auto,w_508,h_320,c_fill/" + restaurant?.imageId} alt="restaurant image" />
<h2>{restaurant?.name}</h2>
<h3>{restaurant?.cuisines.join(", ")}</h3>
<h4>{restaurant?.approxDeliveryTime} minutes</h4>
</div>
)
}
const AppLayout = () => {
return (
<>
<HeaderComponent />
<RestrauntCard restaurant={restaurantDataArray[0]} />
<RestrauntCard restaurant={restaurantDataArray[1]} />
<>
)
}
- We can also destructure
({restaurant})
further:(props) => ({restaurant}) Now, const { imageId, name, cuisines, approxDeliveryTime } = restaurant
const restaurantDataArray = [
{
imageId: "bdcd233971b7c81bf77e1fa4471280eb",
name: "KFC",
cuisines: ["Burger", "Biryani", "American"],
approxDeliveryTime: "45"
},
{
imageId: "bdcd233971b7c81bf77e1fa4471280eb",
name: "Burger king",
cuisines: ["Burger", "American"],
approxDeliveryTime: "15"
}
]
const RestrauntCard = ({restaurant}) => {
const { imageId, name, cuisines, approxDeliveryTime } = restaurant
return (
<div className="restaurant-card">
<img src={"https://res.cloudinary.com/swiggy/image/upload/fl_lossy,f_auto,q_auto,w_508,h_320,c_fill/" + imageId} alt="restaurant image" />
<h2>{name}</h2>
<h3>{cuisines.join(", ")}</h3>
<h4>{approxDeliveryTime} minutes</h4>
</div>
)
}
const AppLayout = () => {
return (
<>
<HeaderComponent />
<RestrauntCard restaurant={restaurantDataArray[0]} />
<RestrauntCard restaurant={restaurantDataArray[1]} />
<>
)
}
Case 5: Passing Sample data(Array) throught Props
[Object destructuring on the fly & passing individual Props]
- Object destructuring on the fly & passing individual Props (without spread operator)
const restaurantDataArray = [
{
imageId: "bdcd233971b7c81bf77e1fa4471280eb",
name: "KFC",
cuisines: ["Burger", "Biryani", "American"],
approxDeliveryTime: "45"
},
{
imageId: "bdcd233971b7c81bf77e1fa4471280eb",
name: "Burger king",
cuisines: ["Burger", "American"],
approxDeliveryTime: "15"
}
]
const RestrauntCard = ({restaurant}) => {
const { imageId, name, cuisines, approxDeliveryTime } = restaurant
return (
<div className="restaurant-card">
<img src={"https://res.cloudinary.com/swiggy/image/upload/fl_lossy,f_auto,q_auto,w_508,h_320,c_fill/" + imageId} alt="restaurant image" />
<h2>{name}</h2>
<h3>{cuisines.join(", ")}</h3>
<h4>{approxDeliveryTime} minutes</h4>
</div>
)
}
const AppLayout = () => {
return (
<>
<HeaderComponent />
<RestrauntCard name={restaurantDataArray[0].name} imageId={restaurantDataArray[0].imageId} cuisines={restaurantDataArray[0].cuisines} approxDeliveryTime={restaurantDataArray[0].approxDeliveryTime} />
<RestrauntCard name={restaurantDataArray[1].name} imageId={restaurantDataArray[1].imageId} cuisines={restaurantDataArray[1].cuisines} approxDeliveryTime={restaurantDataArray[1].approxDeliveryTime} />
<>
)
}
- Object destructuring on the fly & passing individual Props (using spread operator)
const restaurantDataArray = [
{
imageId: "bdcd233971b7c81bf77e1fa4471280eb",
name: "KFC",
cuisines: ["Burger", "Biryani", "American"],
approxDeliveryTime: "45"
},
{
imageId: "bdcd233971b7c81bf77e1fa4471280eb",
name: "Burger king",
cuisines: ["Burger", "American"],
approxDeliveryTime: "15"
}
]
const RestrauntCard = ({restaurant}) => {
const { imageId, name, cuisines, approxDeliveryTime } = restaurant
return (
<div className="restaurant-card">
<img src={"https://res.cloudinary.com/swiggy/image/upload/fl_lossy,f_auto,q_auto,w_508,h_320,c_fill/" + imageId} alt="restaurant image" />
<h2>{name}</h2>
<h3>{cuisines.join(", ")}</h3>
<h4>{approxDeliveryTime} minutes</h4>
</div>
)
}
const AppLayout = () => {
return (
<>
<HeaderComponent />
<RestrauntCard {...restaurantDataArray[0]} />
<RestrauntCard {...restaurantDataArray[1]} />
<>
)
}
const restaurantDataArray = [
{
imageId: "bdcd233971b7c81bf77e1fa4471280eb",
name: "KFC",
cuisines: ["Burger", "Biryani", "American"],
approxDeliveryTime: "45"
},
{
imageId: "bdcd233971b7c81bf77e1fa4471280eb",
name: "Burger king",
cuisines: ["Burger", "American"],
approxDeliveryTime: "15"
}
]
const RestrauntCard = ({restaurant}) => {
const { imageId, name, cuisines, approxDeliveryTime } = restaurant
return (
<div className="restaurant-card">
<img src={"https://res.cloudinary.com/swiggy/image/upload/fl_lossy,f_auto,q_auto,w_508,h_320,c_fill/" + imageId} alt="restaurant image" />
<h2>{name}</h2>
<h3>{cuisines.join(", ")}</h3>
<h4>{approxDeliveryTime} minutes</h4>
</div>
)
}
const AppLayout = () => {
return (
<>
<HeaderComponent />
{/* <RestrauntCard {...restaurantDataArray[0]} /> */}
{/* <RestrauntCard {...restaurantDataArray[1]} /> */}
{restaurantDataArray.map(restaurantDataObj => {
return <RestrauntCard {...restaurantDataObj} />
})}
<>
)
}
- What all
attribute
we pass in React Component when rendering(function call) it, those attributes gets attached to a empty object & this object is know as Props, is passed to that React Component (function defination)
const restaurantData = {
imageId: "bdcd233971b7c81bf77e1fa4471280eb",
name: "KFC",
cuisines: ["Burger", "Biryani", "American"],
approxDeliveryTime: "45"
}
const RestrauntCard = props => {
// console.log(props) // Output: {style: {color: 'red'}, restaurant: {restaurant: {imageId: 'bdcd233971b7c81bf77e1fa4471280eb', name: 'KFC', cuisines: Array(3), approxDeliveryTime: '45'}}}
return (
<div className="restaurant-card">
<img src={"https://res.cloudinary.com/swiggy/image/upload/fl_lossy,f_auto,q_auto,w_508,h_320,c_fill/" + props.restaurant.imageId} alt="restaurant image" />
<h2 style={props.style}>{props.restaurant.name}</h2>
<h3 style={props.style}>{props.restaurant.cuisines.join(", ")}</h3>
<h4 style={props.style}>{props.restaurant.approxDeliveryTime} minutes</h4>
</div>
)
}
const AppLayout = () => {
return (
<>
<HeaderComponent />
<RestrauntCard style={{ color: "red" }} restaurant={restaurantData} />
</>
)
}
const restaurantData = {
imageId: "bdcd233971b7c81bf77e1fa4471280eb",
name: "KFC",
cuisines: ["Burger", "Biryani", "American"],
approxDeliveryTime: "45"
}
const RestrauntCard = ({ imageId, name, cuisines, approxDeliveryTime, style }) => {
return (
<div className="restaurant-card">
<img src={"https://res.cloudinary.com/swiggy/image/upload/fl_lossy,f_auto,q_auto,w_508,h_320,c_fill/" + imageId} alt="restaurant image" />
<h2 style={style}>{name}</h2>
<h3 style={style}>{cuisines.join(", ")}</h3>
<h4style={style}>{approxDeliveryTime} minutes</h4>
</div>
)
}
const AppLayout = () => {
return (
<>
<HeaderComponent />
{/*<RestrauntCard style={{ color: "red" }} restaurant={restaurantData} /> */}
<RestrauntCard style={{ color: "red" }} {...restaurantData} />
</>
)
}