Recompose: Controlled text input using withState throws error

Created on 4 Apr 2016  路  4Comments  路  Source: acdlite/recompose

I'm using withState to manage the value of a controlled input. Here's how i'm using it:

import { compose, withState, mapProps } from 'recompose';

const MyComponent = ({
  originalTitle,
  inputOnChange,
  title
}) => {
  return (
    <div>
      <input type="text" value={ title } onChange={ inputOnChange }/>
    </div>
  );
};

export default compose(
  withState('title', 'changeTitle', (props) => props.originalTitle),
  mapProps((props) => _.assign(props, {
    inputOnChange: (e) => props.changeTitle(() => e.target.value)
  }))
)(MyComponent);

And the error i'm getting is:

Warning: This synthetic event is reused for performance reasons. If you're seeing this, you're accessing the property `target` on a released/nullified synthetic event. This is set to null. If you must keep the original synthetic event around, use event.persist(). See https://fb.me/react-event-pooling for more information.

Uncaught TypeError: Cannot read property 'value' of null

Now, I read about Event Pooling, and saw some recommendations to use event.persist(), but I couldn't figure out how to use it in this setup.
Any help will be greatly appreciated!
Thanks

Most helpful comment

Or just

mapProps((props) => _.assign(props, {
    inputOnChange: (e) => 
     props.changeTitle(e.target.value);
  }))

You can use a simple value in changeTitle

All 4 comments

Ok, so the answer was so simple it's embarrassing, but i'll just post it here and maybe it could help someone else..

mapProps((props) => _.assign(props, {
    inputOnChange: (e) => {
     e.persist();
     props.changeTitle(() => e.target.value);
    }
  }))

Or just

mapProps((props) => _.assign(props, {
    inputOnChange: (e) => 
     props.changeTitle(e.target.value);
  }))

You can use a simple value in changeTitle

Great! that works too. thanks

i don;t konow

Was this page helpful?
0 / 5 - 0 ratings

Related issues

sergej-s picture sergej-s  路  4Comments

franklinkim picture franklinkim  路  3Comments

rockchalkwushock picture rockchalkwushock  路  3Comments

astanciu picture astanciu  路  3Comments

jethrolarson picture jethrolarson  路  4Comments