I have a react component that receives props from the redux store every second. The new state has an array that\'s different than the last array. To be specific, every second an
You're using PureComponent, that do shallow comparison, then your component MyRow
should not be rerendered on each new item being added (Please follow my code example below).
Is there any way to only add the newly added item to the list and not re-render the whole list?
According to your question - Yes, using PureComponent
should render only 1 time the new item:
Here's what the React's docs says:
If your React component’s render() function renders the same result given the same props and state, you can use React.PureComponent for a performance boost in some cases.
PureComponent
:You can check out the code sample, that I did for you.
You will see that the Item
component is always rendered only 1 time, because we use React.PureComponent
. To prove my statement, each time the Item
is rendered, I added current time of rendering. From the example you will see that the Item
Rendered at:
time is always the same, because it's rendered only 1 time.
const itemsReducer = (state = [], action) => {
if (action.type === 'ADD_ITEM') return [ ...state, action.payload]
return state
}
const addItem = item => ({
type: 'ADD_ITEM',
payload: item
})
class Item extends React.PureComponent {
render () {
// As you can see here, the `Item` is always rendered only 1 time,
// because we use `React.PureComponent`.
// You can check that the `Item` `Rendered at:` time is always the same.
// If we do it with `React.Component`,
// then the `Item` will be rerendered on each List update.
return { this.props.name }, Rendered at: { Date.now() }
}
}
class List extends React.Component {
constructor (props) {
super(props)
this.state = { intervalId: null }
this.addItem = this.addItem.bind(this)
}
componentDidMount () {
// Add new item on each 1 second,
// and keep its `id`, in order to clear the interval later
const intervalId = setInterval(this.addItem, 1000)
this.setState({ intervalId })
}
componentWillUnmount () {
// Use intervalId from the state to clear the interval
clearInterval(this.state.intervalId)
}
addItem () {
const id = Date.now()
this.props.addItem({ id, name: `Item - ${id}` })
}
renderItems () {
return this.props.items.map(item => )
}
render () {
return {this.renderItems()}
}
}
const mapDispatchToProps = { addItem }
const mapStateToProps = state => ({ items: state })
const ListContainer = ReactRedux.connect(mapStateToProps, mapDispatchToProps)(List)
const Store = Redux.createStore(itemsReducer)
const Provider = ReactRedux.Provider
ReactDOM.render(
,
document.getElementById('container')
)
MyRow
rerending, please find out what's the reason of rerending, because it should not happen, because of PureComponent
usage.
myList: [ ...state.myList, payload ]
key
to your item component
. If the key
or data
props are changed, then the component will be rerendered. Here are some other libraries, these stand for efficient rendering of lists. I'm sure they will give us some alternatives or insights: