React Props and State Subjective
React Props and State Subjective
Marks:10 * 3 = 30
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={()=>setCount(count+1)}/* TODO: Attach click event
*/}>Increment</button>
</div>
);
};
Partial Code:
import React, { useState } from 'react';
const TodoList = ({ items }) => {
// TODO: Initialize state for completed items
const handleItemClick = (index) => {
// TODO: Implement logic to mark an item as completed
};
return (
<ul>
{/* TODO: Map through items and display them as list items */}
</ul>
);
};
return (
<div>
<label>Name:</label>
<input type="text" value={/* TODO: Bind input value to state */} onChange={/* TODO:
Attach change event */} />
<p>{/* TODO: Display greeting message */}</p>
</div>
);
};
Partial Code:
import React, { useState } from 'react';
return (
<div>
<button onClick={/* TODO: Attach click event */}>Show Message</button>
{ /* TODO: Display the secret message if visible */ }
</div>
);
};
Partial Code:
import React, { useState } from 'react';
return (
<div>
<input type="text" /* TODO: Bind value to state */ />
<button onClick={/* TODO: Attach click event */}>Add</button>
<ul>
{ /* TODO: Map through items and display them as list items */ }
</ul>
</div>
);
};
Partial Code:
import React, { useState } from 'react';
return (
<div>
<h3>What is the capital of France?</h3>
<ul>
<li onClick={() => handleAnswerSelect('Paris')}>Paris</li>
<li onClick={() => handleAnswerSelect('Berlin')}>Berlin</li>
<li onClick={() => handleAnswerSelect('Madrid')}>Madrid</li>
</ul>
{ /* TODO: Display feedback based on the selected answer */ }
</div>
);
};
Partial Code:
import React, { useState } from 'react';
return (
<div>
{labels.map((label, index) => (
<button key={index} onClick={() => handleButtonClick(label)}>
{label}
</button>
))}
{clickedButton && <p>You clicked: {clickedButton}</p>}
</div>
);
};
const people = [
// TODO: Create the Personal Contact];
return (
<div>
// TODO: Display the Personal Contact using map and list
</div>
);
};
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
the data using map and list.
Partial Code:
App.js
import React from 'react';
const people = [
// TODO: Create the Employee Contact];
return (
<div>
// TODO: Pass the Employee data to Detail Component
</div>
);
};
Detail Component.js
import React from 'react';
function DetailComponent( __________)
return (
<div>
// TODO: Employee data to Display
</div>
);
};