Home >Web Front-end >JS Tutorial >Key characteristic of Component-Based Architecture
Here are 5 key characteristics of Component-Based Architecture implemented in React JS. These examples will demonstrate how React components embody the characteristics of
Reusability
Components can be reused across different parts of the application.
Example: A Button component used multiple times
function Button({ label, onClick }) { return <button onclick="{onClick}">{label}</button>; } function App() { return ( <div> <button label="Submit" onclick="{()"> alert('Submit clicked')} /> <button label="Cancel" onclick="{()"> alert('Cancel clicked')} /> </button></button> </div> ); }
Encapsulation
Components encapsulate their logic and styles, preventing outside interference.
Example: UserProfile component encapsulating user data
function UserProfile({ name, email }) { return ( <div> <h3>{name}</h3> <p>Email: {email}</p> </div> ); } function App() { return ( <userprofile name="John Doe" email="john@example.com"></userprofile> ); }
Interchangeability
Components can be swapped or replaced without affecting the overall functionality of the app.
Example: Swapping a PrimaryButton with SecondaryButton
function PrimaryButton({ label, onClick }) { return <button style="{{" backgroundcolor: color: onclick="{onClick}">{label}</button>; } function SecondaryButton({ label, onClick }) { return <button style="{{" backgroundcolor: color: onclick="{onClick}">{label}</button>; } function App({ usePrimary }) { return ( <div> {usePrimary ? <primarybutton label="Click Me" onclick="{()"> alert('Primary clicked')} /> : <secondarybutton label="Click Me" onclick="{()"> alert('Secondary clicked')} />} </secondarybutton></primarybutton> </div> ); }
Scalability
Components make it easy to scale by adding more features without affecting existing components.
Example: Adding more Product components to scale the app
function Product({ name, price }) { return ( <div> <h3>{name}</h3> <p>Price: ${price}</p> </div> ); } function ProductList() { const products = [ { name: 'iPhone 13', price: 999 }, { name: 'Samsung Galaxy S21', price: 799 }, { name: 'Google Pixel 6', price: 599 }, ]; return ( <div> {products.map((product, index) => ( <product key="{index}" name="{product.name}" price="{product.price}"></product> ))} </div> ); } function App() { return <productlist></productlist>; }
Maintainability
Components are isolated, so they can be easily maintained and updated independently.
Example: Updating the Product component without affecting the rest of the app
function Product({ name, price }) { // Add a new feature to show if the product is on sale const isOnSale = price <h3>{name}</h3> <p>Price: ${price} {isOnSale && <span>(On Sale!)</span>}</p> ); } function App() { return ( <div> <product name="Google Pixel 6" price="{599}"></product> </div> ); }
Composition
Components can be combined or composed to build more complex UIs.
Example: Composing Header, Product, and Footer into a single Page
function Header() { return <h1>Welcome to My Shop</h1>; } function Product({ name, price }) { return ( <div> <h3>{name}</h3> <p>Price: ${price}</p> </div> ); } function Footer() { return <footer>Contact us at shop@example.com</footer>; } function Page() { return ( <div> <header></header> <product name="Apple Watch" price="{399}"></product> <footer></footer> </div> ); } function App() { return <page></page>; }
The above is the detailed content of Key characteristic of Component-Based Architecture. For more information, please follow other related articles on the PHP Chinese website!