davnicwil / react-frontload

Async data loading for React components, on client & server
451 stars 21 forks source link

Add option to control when frontload should update #34

Closed LudvigHz closed 5 years ago

LudvigHz commented 5 years ago

This PR adds _experimental_updateFunc to frontloadConnects options, as discussed in #33 . I decided to make this a function instead of an array of prop keys to keep as much logic out of frontload as possible, as well as give users a little more control. updateFunc should take the components prevProps and newProps as arguments and return true if the frontload function should run. This gives the ability to specify exactly when the frontload function should should run on update, without using additional middleware components.

Example: Update only when the id prop changes.


const frontload = (props) => // some function

const updateFunc = (prevProps, newProps) => prevProps.id !== newProps.id

frontloadConnect(frontload, {
    onUpdate: true,
    _experimental_updateFunc: updateFunc
}(component)