React.js useState 钩子导致重新渲染太多,无法更新我的状态
React.js useState hook causes too many re-renders and cant update my state
我是一名正在尝试掌握 React 窍门的学生。到目前为止,我希望能够将从 API 接收到的数据数组剪切成块,以便以后可以使用分页效果。我无法弄清楚为什么函数 arrayChunk() 最终导致渲染过多。如何设置我的函数以将 chunkedArray 推送到 setChunkedYearArray?
这是我的完整代码:
import React, { useState, useEffect } from 'react';
import InformationBox from '../../component/InformationBox/InformationBox';
import arrowButton from '../../assets/arrow-button.svg';
const InformationBoxLayout = (props) => {
const [activeYear, setActiveYear] = useState([]);
const [chunkedYearArray, setChunkedYearArray] = useState([]);
useEffect(() => {
axios
.get('http://www.mocky.io/v2/5ea446a43000005900ce2ca3')
.then((response) =>
setActiveYear(
response.data.timelineInfo.filter((item) => item.year === '2018')
)
);
}, []);
const arrayChunk = (array, chunkSize) => {
const chunkedArray = [];
let clonedArray = [...array];
const splitPieces = Math.ceil(clonedArray.length / chunkSize);
for (let i = 0; i < splitPieces; i++) {
chunkedArray.push(clonedArray.splice(0, chunkSize));
}
setChunkedYearArray(chunkedArray);
};
return (
<div className={style.infoBoxLayoutStyle}>
<button className={style.leftArrow}>
<img src={arrowButton} alt='previous-page-button' />
</button>
{activeYear.length > 6
? arrayChunk(activeYear, 6)
: console.log('no split')}
<button className={style.rightArrow}>
<img
src={arrowButton}
alt='next-page-button'
className={style.rotateArrowRight}
/>
</button>
</div>
);
};
export default InformationBoxLayout;
任何时候您调用 setState 组件都会重新渲染,因此您不能在渲染函数中调用 setState。 chunkedYearArray
没有理由使用状态。只是 return 函数的值然后使用它:
const InformationBoxLayout = props => {
const [activeYear, setActiveYear] = useState([]);
useEffect(() => {
axios
.get("http://www.mocky.io/v2/5ea446a43000005900ce2ca3")
.then(response =>
setActiveYear(
response.data.timelineInfo.filter(item => item.year === "2018")
)
);
}, []);
const arrayChunk = (array, chunkSize) => {
const chunkedArray = [];
let clonedArray = [...array];
const splitPieces = Math.ceil(clonedArray.length / chunkSize);
for (let i = 0; i < splitPieces; i++) {
chunkedArray.push(clonedArray.splice(0, chunkSize));
}
return chunkedArray;
};
// Now you have calculated it, so use it for whatever you want in your JSX
const chunkedYearArray = activeYear.length > 6
? arrayChunk(activeYear, 6)
: [];
return (
<div className={style.infoBoxLayoutStyle}>
<button className={style.leftArrow}>
<img src={arrowButton} alt="previous-page-button" />
</button>
<button className={style.rightArrow}>
<img
src={arrowButton}
alt="next-page-button"
className={style.rotateArrowRight}
/>
</button>
</div>
);
};
export default InformationBoxLayout;
或者,如果您真的希望它处于状态,您可以在 then
中从您的 API 调用中进行计算:
const [chunkedYearArray, setChunkedYearArray] = useState([]);
useEffect(() => {
axios
.get("http://www.mocky.io/v2/5ea446a43000005900ce2ca3")
.then(response =>
setActiveYear(
response.data.timelineInfo.filter(item => item.year === "2018")
)
const arrayChunk = (array, chunkSize) => {
const chunkedArray = [];
let clonedArray = [...array];
const splitPieces = Math.ceil(clonedArray.length / chunkSize);
for (let i = 0; i < splitPieces; i++) {
chunkedArray.push(clonedArray.splice(0, chunkSize));
}
return chunkedArray;
};
const newChunkedYearArray = activeYear.length > 6 ? arrayChunk(activeYear, 6)
: [];
// Because we are in useEffect we are only calculated once!
setChunkedYearArray(newChunkedYearArray)
);
}, []);
这里肯定有清理的余地,但我认为其中任何一个都会让你走上正轨。希望对您有所帮助!
您最终调用 arrayChunk
每次渲染都会设置一些状态并触发重新渲染。当 activeYear
更新应该有所帮助时,使用效果挂钩仅重新计算分块数组。
useEffect(() => {
activeYear.length > 6 && arrayChunk(activeYear, 6);
}, [activeYear]);
然后渲染{ chunkedYearArray }
我是一名正在尝试掌握 React 窍门的学生。到目前为止,我希望能够将从 API 接收到的数据数组剪切成块,以便以后可以使用分页效果。我无法弄清楚为什么函数 arrayChunk() 最终导致渲染过多。如何设置我的函数以将 chunkedArray 推送到 setChunkedYearArray? 这是我的完整代码:
import React, { useState, useEffect } from 'react';
import InformationBox from '../../component/InformationBox/InformationBox';
import arrowButton from '../../assets/arrow-button.svg';
const InformationBoxLayout = (props) => {
const [activeYear, setActiveYear] = useState([]);
const [chunkedYearArray, setChunkedYearArray] = useState([]);
useEffect(() => {
axios
.get('http://www.mocky.io/v2/5ea446a43000005900ce2ca3')
.then((response) =>
setActiveYear(
response.data.timelineInfo.filter((item) => item.year === '2018')
)
);
}, []);
const arrayChunk = (array, chunkSize) => {
const chunkedArray = [];
let clonedArray = [...array];
const splitPieces = Math.ceil(clonedArray.length / chunkSize);
for (let i = 0; i < splitPieces; i++) {
chunkedArray.push(clonedArray.splice(0, chunkSize));
}
setChunkedYearArray(chunkedArray);
};
return (
<div className={style.infoBoxLayoutStyle}>
<button className={style.leftArrow}>
<img src={arrowButton} alt='previous-page-button' />
</button>
{activeYear.length > 6
? arrayChunk(activeYear, 6)
: console.log('no split')}
<button className={style.rightArrow}>
<img
src={arrowButton}
alt='next-page-button'
className={style.rotateArrowRight}
/>
</button>
</div>
);
};
export default InformationBoxLayout;
任何时候您调用 setState 组件都会重新渲染,因此您不能在渲染函数中调用 setState。 chunkedYearArray
没有理由使用状态。只是 return 函数的值然后使用它:
const InformationBoxLayout = props => {
const [activeYear, setActiveYear] = useState([]);
useEffect(() => {
axios
.get("http://www.mocky.io/v2/5ea446a43000005900ce2ca3")
.then(response =>
setActiveYear(
response.data.timelineInfo.filter(item => item.year === "2018")
)
);
}, []);
const arrayChunk = (array, chunkSize) => {
const chunkedArray = [];
let clonedArray = [...array];
const splitPieces = Math.ceil(clonedArray.length / chunkSize);
for (let i = 0; i < splitPieces; i++) {
chunkedArray.push(clonedArray.splice(0, chunkSize));
}
return chunkedArray;
};
// Now you have calculated it, so use it for whatever you want in your JSX
const chunkedYearArray = activeYear.length > 6
? arrayChunk(activeYear, 6)
: [];
return (
<div className={style.infoBoxLayoutStyle}>
<button className={style.leftArrow}>
<img src={arrowButton} alt="previous-page-button" />
</button>
<button className={style.rightArrow}>
<img
src={arrowButton}
alt="next-page-button"
className={style.rotateArrowRight}
/>
</button>
</div>
);
};
export default InformationBoxLayout;
或者,如果您真的希望它处于状态,您可以在 then
中从您的 API 调用中进行计算:
const [chunkedYearArray, setChunkedYearArray] = useState([]);
useEffect(() => {
axios
.get("http://www.mocky.io/v2/5ea446a43000005900ce2ca3")
.then(response =>
setActiveYear(
response.data.timelineInfo.filter(item => item.year === "2018")
)
const arrayChunk = (array, chunkSize) => {
const chunkedArray = [];
let clonedArray = [...array];
const splitPieces = Math.ceil(clonedArray.length / chunkSize);
for (let i = 0; i < splitPieces; i++) {
chunkedArray.push(clonedArray.splice(0, chunkSize));
}
return chunkedArray;
};
const newChunkedYearArray = activeYear.length > 6 ? arrayChunk(activeYear, 6)
: [];
// Because we are in useEffect we are only calculated once!
setChunkedYearArray(newChunkedYearArray)
);
}, []);
这里肯定有清理的余地,但我认为其中任何一个都会让你走上正轨。希望对您有所帮助!
您最终调用 arrayChunk
每次渲染都会设置一些状态并触发重新渲染。当 activeYear
更新应该有所帮助时,使用效果挂钩仅重新计算分块数组。
useEffect(() => {
activeYear.length > 6 && arrayChunk(activeYear, 6);
}, [activeYear]);
然后渲染{ chunkedYearArray }