简体   繁体   English

为什么我的 object 在更新对象的 state 后不会重新渲染?

[英]Why wont my object re-render after updating the object's state?

I'm trying to iterate through my json to create an infinite scroll.我正在尝试遍历我的 json 以创建无限滚动。 On load, I'm showing the first 2 records.在加载时,我正在显示前 2 条记录。 When someone scrolls to the bottom of the page I'm trying to update my object to show current 2 records and append next 2 records to the object.当有人滚动到页面底部时,我正在尝试更新我的 object 以显示当前 2 条记录和 append 下 2 条记录到 object。

I've tried updating the object's state by merging the current object and a new object with the next two records but I keep getting "TypeError: newData.map is not a function" I've tried updating the object's state by merging the current object and a new object with the next two records but I keep getting "TypeError: newData.map is not a function"

function Cards() {

  const [roomsData, setroomsData] = useState([]);
  const [prevData, setprevData] = useState([]);
  const [newData, setnewData] = useState([]);
  const [nextData, setnextData] = useState([]);
  const [startIndex, setstartIndex] = useState(0);
  const [lastIndex, setlastIndex] = useState(2);

useEffect(() => {
     axios.get(`/data/rooms.json`)
       .then(res => {
         const roomDataRes = res.data;
         setroomsData(roomDataRes);
         setprevData(roomDataRes.slice(startIndex,lastIndex));
         setnewData(roomDataRes.slice(startIndex,lastIndex));
    })
  }, []);

//detect end of page scroll
  useEffect(() => {
      window.addEventListener('scroll', handleScroll);
      return () => window.removeEventListener('scroll', handleScroll);
    }, []);

  //what happens after end of page scroll
  function handleScroll() {
    if (window.innerHeight + document.documentElement.scrollTop !== document.documentElement.offsetHeight) return;
    setnextData(newData.slice(2,4));
    setnewData({...prevData,...nextData});
  }

    const cards = newData.map((item,index) =>
      <Card key={index} 
        id={item.id} 
        roomname={item.roomname} 
        location={item.location} 
        zipcode={item.zipcode} 
        images={[item.images]} 
        profile={item.profile} 
        price={item.price} 
        bullets={[item.bullets]} 
        />
      );

  return (
      <div className="cardcontainer">
        {cards}
      </div>
  );
}

export default Cards;

When you scroll to the bottom of the page I'm expecting the cards to update to show the current records and also the next two records, but when you scroll to the bottom I keep getting on the re-render: TypeError: newData.map is not a function当您滚动到页面底部时,我希望卡片会更新以显示当前记录以及接下来的两条记录,但是当您滚动到底部时,我会继续重新渲染: TypeError: newData.map不是 function

The problem is that you're changing newData to an object问题是您将 newData 更改为newData

setnewData({...prevData,...nextData})

use利用

setnewData([...prevData,...nextData])

instead.反而。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么使用React Hooks,MomentJS对象和时间间隔/超时更新状态不会触发重新渲染? - Why does updating state using React Hooks, MomentJS object and interval/timeout not trigger re-render? 为什么在更改 Pinia state(删除 Array 中的一个 object)后不重新渲染 Vue 组件? - Why doesn't re-render Vue the component after changing the Pinia state (deleteing an object in Array)? ReactJS:更新 object state 内的数组不会触发重新渲染 - ReactJS: Updating array inside object state doesn't trigger re-render React js doenst 更新后重新渲染 state - React js doenst re-render after updating state React (Next) 不会在 redux state 更改后重新渲染(是的,state 作为新对象返回) - React (Next) won't re-render after redux state change (yes, state returned as new object) 为什么我的购物车在改变产品状态后没有重新渲染 - why my shopping cart not re-render after the change the state of the product 为什么我的子组件不会在父级的 state 更改上重新渲染? - Why my child components does not re-render on parent's state change? 使用 HTTP 请求获取数据并设置状态后,React Native Screen 不会重新渲染 - React Native Screen wont re-render after fetching data and setting state using HTTP request 为什么在setState之后我的组件不重新渲染? - Why does not my component re-render after setState? 使用 useState 挂钩时,我的 2D 网格不会重新呈现。 有没有办法在我改变里面元素的 object 属性后重新渲染? - my 2D grid does not re-render while using with useState hook. Is there any way to re-render after I change object attributes of the elements inside?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM