0

toggleOn here is a function fired on an onClick event

This was my initial code. the code was working fine in terms altering the data that i wanted to change but somehow react was not rendering it to screen. Also when i went back to my code and save it from there then the component was re-render with updated content

  const toggleOn = (ids) => {
      setboxData(prevBoxData => { 
        prevBoxData.map(item => {
          if(item.id === ids){
            item.on = !item.on;
          } 
          return item;
        })

      return prevBoxData;
      })
    }

Why the code below re-renders my page but not the first one?

 setboxData(prevBoxData => {
        let newData = [];
        prevBoxData.map(item => {
          if(item.id === ids){
            item.on = !item.on;
          } 
          newData.push(item);
        })
      
        return newData;
      })

My whole react component

const Boxes = () => {
    const [boxData, setboxData] = React.useState(Data);

    const toggleOn = (ids) => {
      setboxData(prevBoxData => { 
      
        prevBoxData.map(item => {
          if(item.id === ids){
            item.on = !item.on;
          } 
          return item;
        })

      console.table(prevBoxData);
      })
    }
    
    let boxes = boxData.map((item) => {
      console.log("Item in map: ",item)
      return(  
           <Box bData = {item} handleClick = {toggleOn} />
      )
    })
    
  return (
    <div className='box-container'>
      {boxes}
    </div>
    
  )
}
Shubham
  • 13
  • 5
  • You need to `return` the result of `prevBoxData.map()`, not `prevBoxData` itself – Phil May 29 '23 at 06:10
  • @Phil then i get this error "cannot read properties of undefined (reading 'map')" from my state variable which is mapping throw an array of obects to render some content on page – Shubham May 29 '23 at 06:20
  • The first code is not re-rendering because you are modifying the state in-place. the map function returns a new array, but the objects within that array still reference the same objects in memory.Therefore, when you modify the property of the objects in prevBoxData, React does not detect a change in the state as the reference to prevBoxData remains the same. – Maulik Patel May 29 '23 at 06:20
  • @Shubham you're going to have to [edit] your question to show the code you're now using. You probably have just a typo – Phil May 29 '23 at 06:21
  • @MaulikPatel `map()` returns a new array which should satisfy the state update enough to re-render. The objects within don't necessarily need to be new references (though it doesn't hurt) – Phil May 29 '23 at 06:22
  • You're simply missing a `return` before `prevBoxData.map()` – Phil May 29 '23 at 06:26
  • @Phil yes returning prevBoxData.map() solves it. Thanks – Shubham May 29 '23 at 06:30
  • @MaulikPatel I don't understand this bit " the map function returns a new array, but the objects within that array still reference the same objects in memory." – Shubham May 29 '23 at 06:34

0 Answers0