Write a React component that shows a text value (provided as a prop) with an edit button…
- User
Write a React component that shows a text value (provided as a prop) with an edit button which turns it into an input box that then calls an onChange handler to change the value and go back to text again.
- Assistant
Here's a simple React component that will achieve this functionality:
jsximport React, { useState } from 'react'; const EditableText = ({ text, onChange }) => { const [isEditing, setIsEditing] = useState(false); const [value, setValue] = useState(text); const handleEditClick = () => { setIsEditing(true); }; const handleInputChange = (e) => { setValue(e.target.value); }; const handleInputBlur = () => { setIsEditing(false); onChange(value); }; return ( <div> {isEditing ? ( <input type="text" value={value} onChange={handleInputChange} onBlur={handleInputBlur} /> ) : ( <span>{value}</span> )} <button onClick={handleEditClick} disabled={isEditing}> Edit </button> </div> ); }; export default EditableText;
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.