question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

SearchInput onChange callback has a null event.target

See original GitHub issue

Description

Moving this to the callback of setState seems to have the side-effect of causing event.target to be null for the onChange callback. So something like

<SearchInput
  onChange={e => console.log(e.target.value)}
/>

throws TypeError: Cannot read property 'value' of null

Here’s a stack overflow I found on related to the issue: https://stackoverflow.com/questions/51256318/issue-of-using-e-target-value-in-react-setstate-function

Versions

fundamental-react: latest fundamental-styles: latest


NOTE: Where applicable, please include uncropped screen captures.

DISCLAIMER: After triaging an issue, the fundamental-react team will see if it can be reproduced or confirmed. If more information is needed, the fundamental-react team will contact the author. Any issues awaiting responses from the author for more than 7 days will be closed. The author can re-open the issue at a later time if they can present the requested information.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:1
  • Comments:5 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
singh-mahendracommented, Mar 10, 2020

If we use event.persist();, we would be effectively disabling a React optimization for events. Moreover,this problem is present in all components where the event handlers are passed in this manner. Using event.persist() for all the components does not seem to be a good idea. A simple solution would be to convert these components to ForwardRefs and the developer can access the event.target through ref,leaving the event optimization as is. @jbadan Please advice.

1reaction
skvalecommented, Jun 26, 2019

I did verify that adding event.persist(); does fix the problem

        event.persist();
        this.setState({
            value: event.target.value,
            isExpanded: true,
            filteredResult: filteredResult
        }, () => {
            this.props.onChange(event);
        });
Read more comments on GitHub >

github_iconTop Results From Across the Web

Event target is null inside functional setState - Stack Overflow
If I uncomment the first line inside handleInputChange function, where I store text box value inside val variable, it works well. Ideas why?...
Read more >
Null event properties in a React callback - Ludovico Fischer
event.target is null inside the callback because React recycles events objects for performance reasons: React passes the same Event object for ...
Read more >
Event.currentTarget - Web APIs | MDN
The currentTarget read-only property of the Event interface identifies the current target for the event, as the event traverses the DOM.
Read more >
Debouncing events with React - Medium
debounce function ensures that the actual onChange event callback is called only when the user has stopped inputting the characters for 300ms.
Read more >
Input - Ant Design
A search input is required. ... bordered, Whether has border style, boolean, true, 4.5.0 ... onChange, Callback when user input, function(e), -.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found