Skip to content

Latest commit

History

History
63 lines (52 loc) 路 2.39 KB

File metadata and controls

63 lines (52 loc) 路 2.39 KB

(MNA.1) Managing arrays

CodeSandbox

https://codesandbox.io/s/mna1-57em4?file=/src/App.tsx

Statement

馃敼 Create a parent component and in its state store the array of names (strings). Initial value - ["First", "Second"]
馃敼 Create a child component with the following props

interface ChildProps {
    name: string;
    index: number;
    onChange: (index: number, newName: string) => void;
    onDelete: (index: number) => void;
}

馃敼 Parent component should render child components (each child component responsible for rendering one name) and a button, which, when clicked, will add new name at the end. It should pass required props to child components and the passed functions must behave as their names suggest.
馃敼 Child component should render controllable input with explicit value passed from parent. Changing input value must call onChange function (received from props) with appropriate arguments. Render button for deleting too.
馃敼 Make sure that changing value of single name rerenders only parent and corresponding child component

(MNA.2) Managing uncontrollable components

CodeSandbox

https://codesandbox.io/s/mna2-ry93s?file=/src/App.tsx

Statement

The problem is same as MNA.1, except:
馃敼 Use uncontrollable input in child component. That is, instead of passing value to input, pass defaultValue.
馃敼 Notice the bug when deleting non-last child component.
馃敼 Resolve the bug. You are not allowed to change the types of ChildProps or names (string[]). However, you are free to introduce new states (or properties in state in case of class components).

(MNA.3) Managing arrays of more general child components

CodeSandbox

https://codesandbox.io/s/mna3-2j0xl?file=/src/App.tsx

Statement

The problem is same as MNA.1, except:
馃敼 the props of child component. The new type should be

interface ChildProps {
    name: string;
    onChange: (newName: string) => void;
    onDelete: () => void;
}

馃敼 Try achieving the desired results without considering the optimization (of rerendering only the appropriate components).
馃敼 Finally, try achieving desired result with optimizing rerendering as described in MNA.1.