⊗jsrtPmCpMVS 90 of 112 menu

Modes of operation via states of child components in React

Let our array of products now look like this:

const initProds = [ {id: id(), name: 'prod1', cost: 'cost1', catg: 'catg1'}, {id: id(), name: 'prod2', cost: 'cost2', catg: 'catg2'}, {id: id(), name: 'prod3', cost: 'cost3', catg: 'catg3'}, ];

Let's output these products as an HTML table. At the same time, we will make it so that when you click on any cell of the table, an input for editing appears in this cell. To solve the problem, we will make 3 components.

The Products component will store the state with products and use the Product components to output products. The Product component in turn will also use the ProductField components to output a specific product field (name, price, category).

The ProductField component will either show the field text or an input for editing it. In this case, the editing or display mode is controlled by the state of this component.

That is, we will not store the mode in the parent state. It would be very inconvenient there, since we would have to specify the mode for each product field, which would turn our state into something like this:

const initProds = [ [ {field: 'name', value: 'prod1', isEdit: false}, {field: 'cost', value: 'cost1', isEdit: false}, {field: 'catg', value: 'catg1', isEdit: false}, ], [ {field: 'name', value: 'prod2', isEdit: false}, {field: 'cost', value: 'cost2', isEdit: false}, {field: 'catg', value: 'catg2', isEdit: false}, ], [ {field: 'name', value: 'prod3', isEdit: false}, {field: 'cost', value: 'cost3', isEdit: false}, {field: 'catg', value: 'catg3', isEdit: false}, ], ]

However, we will not make such a state, but leave the one that was. Simply, each instance of the ProductField component will regulate the mode with the help of its state: either editing or showing.

Thus, it turns out that the parent component will store a state with data, and our grandchild component will receive this data through props and will have its own state for changing its mode.

So, let's implement what was described.

Component Products

function Products() { const [prods, setProds] = useState(initProds); function changeField(id, field, event) { setProds(prods.map(prod => { if (prod.id === id) { prod[field] = event.target.value; } return prod; })); } const rows = prods.map(prod => { return <Product key ={prod.id} id ={prod.id} name={prod.name} cost={prod.cost} catg={prod.catg} changeField={changeField} />; }); return <div> <table> <tbody> {rows} </tbody> </table> </div>; }

Product Component

function Product({ id, name, cost, catg, changeField }) { return <tr> <ProductField id={id} text={name} type="name" changeField={changeField} /> <ProductField id={id} text={cost} type="cost" changeField={changeField} /> <ProductField id={id} text={catg} type="catg" changeField={changeField} /> </tr>; }

ProductField Component

function ProductField({ id, text, type, changeField }) { const [isEdit, setIsEdit] = useState(false); return <td> { isEdit ? <input value={text} onChange={event => changeField(id, type, event)} onBlur={() => setIsEdit(false)} /> : <span onClick={() => setIsEdit(true)}>{text}</span> } </td>; }

Practical tasks

Perform similar operations with the Users and User components you created in the previous lessons.

English
AfrikaansAzərbaycanБългарскиবাংলাБеларускаяČeštinaDanskDeutschΕλληνικάEspañolEestiSuomiFrançaisहिन्दीMagyarՀայերենIndonesiaItaliano日本語ქართულიҚазақ한국어КыргызчаLietuviųLatviešuМакедонскиMelayuမြန်မာNederlandsNorskPolskiPortuguêsRomânăРусскийසිංහලSlovenčinaSlovenščinaShqipСрпскиSrpskiSvenskaKiswahiliТоҷикӣไทยTürkmenTürkçeЎзбекOʻzbekTiếng Việt
We use cookies for website operation, analytics, and personalization. Data processing is carried out in accordance with the Privacy Policy.
accept all customize decline