|
| 1 | +# For |
| 2 | +Component to optimize the rendering of a list of elements without need to specify a key value for all elements, and other options. [See demo](https://nDriaDev.io/react-tools/#/components/For) |
| 3 | + |
| 4 | +## Usage |
| 5 | + |
| 6 | +```tsx |
| 7 | +export default function ForComponent() { |
| 8 | + const arr = useRef([ |
| 9 | + { id: "1", firstName: "Jhon", lastName: "Doe" }, |
| 10 | + { id: "2", firstName: "Jona", lastName: "Doe" }, |
| 11 | + { id: "3", firstName: "Jhonney", lastName: "Doe" } |
| 12 | + ]); |
| 13 | + const [, update] = useReducer(t => !t, false); |
| 14 | + |
| 15 | + const body = useCallback<(item: { id: string, firstName: string, lastName: string }, index: number|string) => JSX.Element>((item, index) => { |
| 16 | + console.log("body render"); |
| 17 | + return <p>{index}: {item.firstName} - {item.lastName}</p> |
| 18 | + }, []); |
| 19 | + |
| 20 | + useEffect(() => { |
| 21 | + const id = setInterval(() => update(), 1000); |
| 22 | + return () => clearInterval(id) |
| 23 | + }, []) |
| 24 | + |
| 25 | + return <> |
| 26 | + <For |
| 27 | + elementKey="id" |
| 28 | + of={arr.current} |
| 29 | + > |
| 30 | + {body} |
| 31 | + </For> |
| 32 | + </> |
| 33 | +} |
| 34 | +``` |
| 35 | + |
| 36 | +> The component uses _For_ component to render a p element returned from _body_ memoized function for all objects assigned to _arr_ ref variable. It also specified __id__ property as _elementKey_ prop. A setInverval is executed on mount that on every second force component to rerender. If you open dev tools, you can see that _body_ function logs only three times at first, one for each element of _arr_ variable |
| 37 | +
|
| 38 | + |
| 39 | +## API |
| 40 | + |
| 41 | +```tsx |
| 42 | +Formemo(<T extends unknown>({ of, children, filter, map, sort, elementKey, fallback }: ForProps<T>) |
| 43 | +``` |
| 44 | + |
| 45 | +> ### Params |
| 46 | +> |
| 47 | +> - __props__: _ForProps<T>_ |
| 48 | +component properties object. |
| 49 | +> - __props.of__: _T[]_ |
| 50 | +array of elements. |
| 51 | +> - __props.elementKey?__: _T extends Record<string,unknown> ? keyof T : never_ |
| 52 | +a key of array elements if elements are object. |
| 53 | +> - __props.children__: _(item: T, index: T extends Record<string,unknown> ? number | T[keyof T] : number) => ReactNode_ |
| 54 | +it's a function that takes the current item as first argument and optionally a second argument that is number if element of array aren't object, otherwise it can be a number or the value of the element key specified in the _elementKey_ prop if it is preset. |
| 55 | +> - __props.fallback?__: _ReactNode_ |
| 56 | +optional element to render when _of_ prop is an empty array. |
| 57 | +> - __props.filter?__: _<S extends T>(val: T, index: number, arr: T[]) => val is S_ |
| 58 | +callback executed to filter _of_ elements. |
| 59 | +> - __props.map?__: _<U extends T>(val: T, index: number, arr: T[]) => U_ |
| 60 | +callback executed to map _of_ elements. |
| 61 | +> - __props.sort?__: _(a: T, b: T) => number_ |
| 62 | +callback executed to sort _of_ elements. |
| 63 | +> |
| 64 | + |
| 65 | +> ### Returns |
| 66 | +> |
| 67 | +> __result__: elements list, rendered from _of_ prop or _fallback_. |
| 68 | +> - _JSX.Element_ |
| 69 | +> |
0 commit comments