ianstormtaylor / react-values

A set of tiny React components for handling state with render props.
https://git.io/react-values
MIT License
1.02k stars 39 forks source link

Memoize render props #6

Closed theKashey closed 6 years ago

theKashey commented 6 years ago

In my experience it is common to pass set from Any down to the components, but the value will be updated on every value change, as long it is creating on every render.

Probably, creating some factory to produce Component with memoized callback is a better way, and it shall not increase the size of this library.

like

const ArrayValue = produce({
        speread: value => value,
        actions: {
           first: value => value.value[0],
           last: value => value.value[Math.max(0, value.value.length - 1)],
           clear: value => () => value.set([]),
           concat: value => proxy(value, 'concat'),
           fill: value => proxy(value, 'fill'),
           filter: value => proxy(value, 'filter'),
           .......
      }
    })
)
ianstormtaylor commented 6 years ago

@theKashey I agree! I think it's a bit strange that we're currently re-creating the functions for every render.

theKashey commented 6 years ago

Wow! That was fast! 🚀

ianstormtaylor commented 6 years ago

Released in 0.2.0!