React Props and State Descriptive (21-11-2023)
React Props and State Descriptive (21-11-2023)
1.Create a React component that displays a counter. The counter should start at 0 and increment by
1 each time a button is clicked. Use state to manage the counter value.
Partial code:
return (
<div>
<p>Counter: {/* TODO: Display counter value */}</p>
<button onClick={/* TODO: Attach click event */}>Increment</button>
</div>
);
};
Answer:
import React, { useState } from 'react';
const Counter = () => {
const [counter, setCounter] = useState(0);
return (
<div>
<p>Counter: {counter}</p>
<button onClick={incrementCounter}>Increment</button>
</div>
);
};
2.Create a React component that takes a list of items as props and displays them in an unordered
list. Allow users to click on an item to mark it as "completed" and style completed items differently.
Partial Code:
};
return (
<ul>
{/* TODO: Map through items and display them as list items */}
</ul>
);
};
Answer:
setCompletedItems(updatedCompletedItems);
};
return (
<ul>
<li
key={index}
>
{item}
</li>
))}
</ul>
);
};
3. Create a React component that allows users to input their name and displays a greeting message.
Use state to manage the input value.
Patial Code:
};
return (
<div>
<label>Name:</label>
<input type="text" value={/* TODO: Bind input value to state */} onChange={/* TODO: Attach
change event */} />
</div>
);
};
Answer:
import React, { useState } from 'react';
setName(event.target.value);
};
return (
<div>
<label>Name:</label>
</div>
);
};
Partial Code:
return (
<div>
{/* TODO: Map through colors and display colored squares */}
</div>
);
};
Answer:
return (
<div>
<div
key={index}
style={{ backgroundColor: color, width: '50px', height: '50px', margin: '5px', display: 'inline-
block' }}
></div>
))}
</div>
);
};
Partial Code:
};
return (
<div>
</div>
);
};
setIsVisible(!isVisible);
};
return (
<div>
</div>
);
};
Partial Code:
};
return (
<div>
<ul>
</ul>
</div>
);
};
setItems([...items, newItem]);
setNewItem('');
};
return (
<div>
<input
type="text"
value={newItem}
/>
<button onClick={addItem}>Add</button>
<ul>
<li key={index}>{item}</li>
))}
</ul>
</div>
);
};
Partial Code:
};
return (
<div>
<ul>
</ul>
</div>
);
};
setSelectedAnswer(answer);
};
return (
<div>
<ul><li
>
Paris
</li> <li
>
Berlin
</li> <li
>
Madrid
</li> </ul>
{selectedAnswer && (
)}
Partial Code:
};
return (
<div>
{label}
</button>
))}
</div>
);
};
setClickedButton(label);
};
return (
<div>
{label}
</button>
))}
</div>
);
};
take an array of objects with personal information and display the data using map and list.
Partial Code:
const people = [
return (
<div>
</div>
);
};
const people = [
];
return (
<div>
<h1>Personal Information</h1>
<ul>
{people.map(person => (
<li key={person.id}>
))}
</ul>
</div>
);
};
10. 10. Create a React component that displays a personal information. The App component
should take an array of objects with personal information and the DetailsComponent display
Partial Code:
App.js
const people = [
return (
<div>
</div>
);
};
Detail Component.js
return (
<div>
// TODO: Employee data to Display
</div>
);
};
App.js
const employees = [
];
return (
<div>
<h1>Employee Information</h1>
</div>
);
};
return (
<div>
<h2>Employee Details</h2>
<ul>
{employees.map(employee => (
<li key={employee.id}>
</li>
))}
</ul>
</div>
);