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.

Warn when input's value is null

See original GitHub issue

Passing null indicates that the user tried to specify a value (eg. from the database) but didn’t notice the value was null; seems like that should be a warning. Normally we treat null and undefined as the same, but in this case passing null indicates an error and should therefore be discouraged. The user should decide if they want an uncontrolled component (in which case they should pass undefined) or if they want an empty controlled component (in which case they should pass the empty string).

@spicyj said he would be fine with this being a warning and/or with treating null as an empty string. Making it an error/warning has the advantage that there is now an easy upgrade path (fix the warning) without us introducing subtle changes in behavior that will break people’s apps.

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Comments:17 (12 by maintainers)

github_iconTop GitHub Comments

4reactions
IndifferentDisdaincommented, Oct 31, 2017

So… why is this a thing? null is a perfectly acceptable initial value. For example, when creating a new object (initialized w/ default values from the server then passed to the component as props) in a form that requires address, Address Line 2 is often optional. I’m perfectly aware that it’s null, and I may or may not add a value to it when completing the form.

I do the suggested workaround <input value={foo || ''} onChange={this.handleChange} />, but it just seems hackey. From what I can tell on v15.6.1, the only issue this causes is the console warning. I know I can just ignore the warning, but it’s distracting when debugging actual errors.

3reactions
gaearoncommented, Oct 31, 2017

I think the first post is a bit misleading.

There’s no problem with null as a value. The problem is React treats this as a flag to make an input uncontrolled. That turned out to be confusing.

So we added a warning to discourage existing users from passing null. This lets us safely change the behavior for future version to treat null as an empty string. We haven’t done this yet but that’s the plan, if I understand it right.

Maybe now is a good time to do this. Do you mind raising a new issue to discuss?

Read more comments on GitHub >

github_iconTop Results From Across the Web

After JavaScript alert value is null - jquery - Stack Overflow
When I am doing JavaScript alert after that value is been null from the input text. But I want that after the JavaScript...
Read more >
JavaScript : HTML form - Checking for non empty - w3resource
A zero-length string or a NULL value. Javascript function to check whether a field is empty or not // If the length of...
Read more >
i added alert box if input is empty string but there is a problem ...
The issue here is that you are setting the value of input.value to an empty string before testing to see if input.value is...
Read more >
Resolve nullable warnings | Microsoft Learn
Several compiler warnings indicate code that isn't null-safe. Learn how to address those warnings by making your code more resilient.
Read more >
Video: Input and error messages - Microsoft Support
Select the cells that you want to create a message for, and click Data Validation. On the Input Message tab, check the box...
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