here is my render function in React. I have a problem that when I click on…
- User
here is my render function in React. I have a problem that when I click on FunctionButton component and it renders more element from the array it renders it within the SLineWrapper component. What I want is each time I click FunctuiButton component the new portion of elements will be rendered in a new SLineWrapper component. What changes do I need to make into render function? render: (_, employee: IEmployee, index) => ( // console.log(
employee #${index}:, employee.405184) // // {employee.skillGroups.map((group) => ( // {group.name} // )) ?? ''} // , <SSkillGroupCellWrapper data-testid={'cell-wrapper'}> <SLineWrapper data-testid={'line-wrapper'}> {skillGroupsMock.slice(0, sliceEnd).map((group) => ( <STagWrapper data-testid={'my-tag-wrapper'}> <Tag maxTagWidth={'100%'} size="small"> {group.name} )) ?? ''} {sliceEnd <= countProperties(skillGroupsMock) && ( <FunctionButton onClick={() => { setSliceEnd(sliceEnd + PART_OF_ELEMENTS); }} > {ещё ${ countProperties(skillGroupsMock) - sliceEnd >= PART_OF_ELEMENTS ? PART_OF_ELEMENTS : countProperties(skillGroupsMock) - sliceEnd }} )} ), - Assistant
To render each new portion of elements in a new SLineWrapper component each time the FunctionButton is clicked, you can create a new array that holds the elements for each SLineWrapper component. Here's how you can update your render function:
render: (_, employee: IEmployee, index) => { const [sliceEnd, setSliceEnd] = useState(PART_OF_ELEMENTS); const skillGroups = employee.skillGroups; // Calculate the number of SLineWrapper components const numWrappers = Math.ceil(skillGroups.length / PART_OF_ELEMENTS); // Create an array of arrays, each containing a portion of elements const slicedSkillGroups = Array.from({ length: numWrappers }, (v, i) => skillGroups.slice(i * PART_OF_ELEMENTS, (i + 1) * PART_OF_ELEMENTS) ); return ( <SSkillGroupCellWrapper data-testid={'cell-wrapper'}> {/* Render a SLineWrapper component for each array slice */} {slicedSkillGroups.map((slice, i) => ( <SLineWrapper key={i} data-testid={'line-wrapper'}> {slice.map((group) => ( <STagWrapper key={group.name} data-testid={'my-tag-wrapper'}> <Tag maxTagWidth={'100%'} size="small"> {group.name} </Tag> </STagWrapper> ))} </SLineWrapper> ))} {sliceEnd <= skillGroups.length && ( <FunctionButton onClick={() => { setSliceEnd(sliceEnd + PART_OF_ELEMENTS); }} > {`ещё ${ skillGroups.length - sliceEnd >= PART_OF_ELEMENTS ? PART_OF_ELEMENTS : skillGroups.length - sliceEnd } `} </FunctionButton> )} </SSkillGroupCellWrapper> ); } - User
can you redo this code of yours but make all code to be of ES6 standard?
- Assistant
Sure! Here's the updated code using ES6 syntax:
const render = (_, employee: IEmployee, index) => { const [sliceEnd, setSliceEnd] = useState(PART_OF_ELEMENTS); const skillGroups = employee.skillGroups; const numWrappers = Math.ceil(skillGroups.length / PART_OF_ELEMENTS); const slicedSkillGroups = Array.from({ length: numWrappers }, (v, i) => skillGroups.slice(i * PART_OF_ELEMENTS, (i + 1) * PART_OF_ELEMENTS) ); return ( <SSkillGroupCellWrapper data-testid="cell-wrapper"> {slicedSkillGroups.map((slice, i) => ( <SLineWrapper key={i} data-testid="line-wrapper"> {slice.map((group) => ( <STagWrapper key={group.name} data-testid="my-tag-wrapper"> <Tag maxTagWidth="100%" size="small"> {group.name} </Tag> </STagWrapper> ))} </SLineWrapper> ))} {sliceEnd <= skillGroups.length && ( <FunctionButton onClick={() => { setSliceEnd(sliceEnd + PART_OF_ELEMENTS); }} > {`ещё ${ skillGroups.length - sliceEnd >= PART_OF_ELEMENTS ? PART_OF_ELEMENTS : skillGroups.length - sliceEnd } `} </FunctionButton> )} </SSkillGroupCellWrapper> ); }