
ReactJS Anti Pattern ~Passing Setters Down the Components Tree~
・This pattern allows developers to pass the setter function directly to its child component. // Form.jsx function Form() { const [formData, setFormData] = useState({ name: '' }); return ( <div> <h1>Form</h1> {/* Pass the setter function down to ChildComponent */} <Input name={formData.name} setFormData={setFormData} /> <button onClick={() => console.log(formData)}>Submit</button> </div> ); }; // Input.jsx function Input({ name, setFormData }) { const handleInputChange = (event) => { // Directly using the setFormData setter function from the parent setFormData((prevData) => ({ ...prevData, name: event.target.value })); }; return ( <div> <label> Name: <input type="text" value={name} onChange={handleInputChange} /> </label> </div> ); }; Abstraction Leak ・An abstraction leak occurs when one component knows too much about another component's internal implementation. In this case, the Input component makes the following assumption: The parent component uses the useState hook. The state conta
Continue reading on Dev.to
Opens in a new tab



