Open In App

What are some advantages of Component Driven Development ?

Component Driven Development (CDD) is a fundamental feature in ReactJS, playing a significant role in our preference for building applications with the React library. The essence of CDD lies in the segmentation of large file content into smaller, reusable parts that find application across multiple areas.

These are some advantages of the Component Driven Development in React:



Prerequisites:

Steps to create React Application And Installing Module:

Step 1: Create a React application using the following command:

npx create-react-app example

Step 2: After creating your project folder i.e. example, move to it using the following command:



cd example

Step 3: Create folder components inside src folder of react project directory and inside the components folder create files Button.jsx, Card.jsx and List.jsx.

Project Structure:

folder structure

The updated dependencies in package.json file will look like:

"dependencies": {
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-scripts": "5.0.1",
"web-vitals": "^2.1.4",
}

Example: Write down the following code in App.js, and components folder files.




//App.jsx
 
import React from 'react'
import Card from './components/Card'
 
const App = () => {
    return (
        <div style={{
            display: 'flex',
            flexDirection: 'column',
            alignItems: 'center',
            justifyContent: 'space-around',
            marginTop: '20px',
            height: '100vh',
            width: '100vw',
        }}>
            <div>Components based design</div>
            <div style={{
                display: 'flex',
                flexDirection: 'row',
                justifyContent: 'space-around',
                width: '100%',
                height: '100%',
                marginTop: '20px',
            }}>
                <Card />
                <Card />
                <Card />
                <Card />
            </div>
        </div>
    )
}
 
export default App




//Button.jsx
 
import React from 'react'
 
const Button = () => {
    return (
        <div
            style={{
                backgroundColor: 'green',
                color: 'white',
                padding: '10px',
                borderRadius: '5px',
                cursor: 'pointer',
                margin: '10px'
            }}
            onClick={() => alert('Button clicked')}
        >Button</div>
    )
}
 
export default Button




//Card.jsx
 
import React from 'react'
import List from './List'
import Button from './Button'
 
const Card = () => {
  return (
    <div style={{
      height: '200px',
      width: '200px',
      backgroundColor: '#f4f4f4',
      borderRadius: '5px',
      margin: '10px',
      display: 'flex',
      flexDirection: 'column',
      justifyContent: 'space-between',
      alignItems: 'center',
import React from 'react'
import List from './List'
import Button from './Button'
 
const Card = () => {
    return (
        <div style={{
            height: '200px',
            width: '200px',
            backgroundColor: '#f4f4f4',
            borderRadius: '5px',
            margin: '10px',
            display: 'flex',
            flexDirection: 'column',
            justifyContent: 'space-between',
            alignItems: 'center',
            boxShadow: '0px 0px 5px #ccc'
 
        }}>
            <List />
            <Button></Button>
        </div>
    )
}
 
export default Card
      boxShadow: '0px 0px 5px #ccc'
 
    }}>
      <List />
      <Button></Button>
    </div>
  )
}
 
export default Card




//List.jsx
 
import React from 'react'
 
const List = () => {
    return (
        <div>List item</div>
    )
}
 
export default List

Step to Run the Application: Run the application using the following command:

npm start

Output:

Output


Article Tags :