Reagent mutation props

I have a React component that receives an array or objects via props . Something I would like to do is reorder events with these details. However, it seems that re-rendering of React only occurs when state changes.

Currently, my ordering is processed in the parent object and a method is passed to process the order through support, but ideally I want the component responsible for rendering these objects to process the order as well.

Chucking props in state seems bad, but what's the best thing to do here?

+6
source share
2 answers

The details are immutable, but in your case it seems that your data does not change, only the sort order changes, so you can:

  • save your data and sorting functions as props
  • keep sort order in state
  • maybe use getInitialState to return the default sort order
  • when the sort order is changed, the state is set so that reprocessing is performed.
+7
source

As I understand it, React needs to change state to trigger a render. If you want the sorting logic to be attached to you, you need to create a copy of the array.

 getInitialState: function () { return { items: this.props.items.slice(0) }; }, 

As implemented here.

+1
source

Source: https://habr.com/ru/post/984498/


All Articles