38
loading...
This website collects cookies to deliver better user experience
Card
componentimport React from "react"
const card = (props) => {
// return some html code here
}
export default card;
Profile
page by simply doing these two linesimport Card from "./components/card"
<Card ...props />
Card
elements will be changed, really powerful, or is it?const card = (props) => {
if(props.isAdmin) {
// render admin badge here
// other elements for admin
}
// return some html code here
}
export default card;
<Card isAdmin={admin value} />
border
of the element based if the user has been a long time user (they have been created more than one month ago). Let's update the code once moreconst card = (props) => {
if(props.isAdmin) {
// render admin badge here
// other elements for admin
}
if(props.isUserActive) {
// change border of the card element
}
// return some html code here
}
export default card;
const card = (props) => {
if(props.isAdmin) {
// render admin badge here
// other elements for admin
}
if(props.isModerator) {
// update UI elements for moderator
// a border and specific icon
}
if(props.isUserActive) {
// change border of the card element
}
// return some html code here
}
export default card;
if
statement. I've been guilty of this, i've had components perform multiple checks and multiple control props just to determine how i'm going to render the component.if
statements by placing each if
in it's own file, we would end up with three components//components/cards/admin.js
import React from "react"
const adminCard = (props) => {
// all admin properties
// admin icon
// admin badge
// returns html for an admin card.
}
export default adminCard;
//components/cards/moderator.js
import React from "react"
const moderatorCard = (props) => {
// all moderator properties
// moderator icon
// moderator badge
// returns html for an moderator card.
}
export default moderatorCard;
//components/cards/activeUser.js
import React from "react"
const activeUserCard = (props) => {
// all active user properties
// active user icon
// active user badge
// returns html for an moderator card.
}
export default activeUserCard;
<AdminCard isAdmin={admin value} />
<ActiveUserCard ...props />
<ModeratorCard ...props />
if
controlled code into a file with standalone purpose, but we ended up copy-pasting the code into three different files.