getInitialState() => clone(props)?

半世苍凉 提交于 2019-12-25 04:07:05

问题


We have the following React Component Hierarchy on a particular page.

<Page>
  ....
  <GoogleMap>   ....      </GoogleMap>
  ....
  <BoxList>
     <Box>(lazy loaded 'n' at a time as you scroll and hit bottom, upto 400) 
       ......(Some other box info that needs to be updated incrementally)
       <ItemList>
         <Item> (Upto 20 per Box/ItemList)
           ......(Some item Info that needs to be updated incrementally)
         </Item> 
         <Item>...</Item>
         ....  
       </ItemList>   
     </Box>
     <Box>....</Box>
     ....
  </BoxList>
</Page>

On page load and as the user scrolls down, the top most component(Page) fetches the json for the next 'n' ( say, 10 ) Boxes and the data is passed all the way down to the (Box > ItemList > Item) as props and they get rendered.

We also receive small, incremental updates via pusher which need to be applied on to Box and Item components as they arrive. Since we were not sure how to go about this the React way, we are doing it the following way for Items.

a) Item components don't have any state as React docs recommend moving state into the immediate shared parent/ancestor component.

b) getInitialState() in ItemList simply clones this.props to generate its initial state. this.props is never used in the render function, it relies solely on this.state to render.

c) componentDidMount() in ItemList sets up listeners for Pusher updates and the handlers simply perform a setState({ data: data.merge(incremental_update)}) which trigger a re-render and new data is then passed down to Item as new props and are re-rendered.

Since there are a lot of ItemLists on the page ( upto 400 ) and since this.props is never really used by ItemList to render, what would be a better way to achieve this ? This app uses almost double the memory(100 MBs+ when all boxes are loaded) when compared to our original mutate-the-dom-with-jquery implementation(~50 MBs when all boxes are loaded) and becomes unusable on thin clients.

A related question that was the pre-cursor to this question.

来源:https://stackoverflow.com/questions/28435915/getinitialstate-cloneprops

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!