我想填充数组状态,但 projectItems 在 useState 中没有更新,当我放置 projectItems 时,组件会重新渲染。所以,我正在使用usecallback,但是当我将filteredItems和projectItems作为useCallback中的依赖数组和useState中的回调时,useCallback永远不会运行。
const [filteredItems, setfilteredItems] = useState([]);
const [projectItems, setProjectItems] = useState([]);
const { projects } = props;
const callback = useCallback(() => {
console.log('callback');
const projectData = projects.map(i => {
return (
{
key: '1',
projectName: i.project_name,
size: i.size,
location: i.location,
status: `Day ${i.Finished_events_days}/${i.total_days_needed}`,
score: `${i.score}/10`,
view: <a>View Project</a>,
download: <a>Download</a>,
feedback: <a>Feedback</a>,
audit: <a>Audit</a>
})
});
setProjectItems(prevState => [...prevState, ...projectData]);
console.log(projectItems);
setfilteredItems([...projectItems]);
}, [projectItems, filteredItems]);
useEffect(() => {
}, [callback]);