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.

Can't seem to use null as a default value?

See original GitHub issue

I have a custom input component that declares:

Component.propTypes = {
  value: PropTypes.number
};

Because mobx-react-form defaults the value to an empty string, I’m getting proptype errors in the console. So, I figured I could override the default value and set it to null, but an empty string still seems to be getting passed in. I tried default: null, initial: null, and value: null, but nothing worked.

I’ve tried looking for documentation about default values, but I couldn’t seem to find much of anything. Sorry for asking questions in the issue tracker 😄 .

At the risk of more configuration boilerplate, do you think it would it be best for users to provide their own default values instead of assuming an empty string?

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:10 (5 by maintainers)

github_iconTop GitHub Comments

9reactions
jkeruzeccommented, Jun 22, 2018

For the record, I make it work with this :

simulationDate: {
                    label: 'Simulation date',
                    placeholder: 'Simulation date'+ '..',
                    rules: 'required|date',
                    value:'',
                    input: value => value === '' ? null : value, 
                    output: value => value === '' ? null : value
                }
3reactions
rzanecommented, Dec 4, 2016

So in the case of <input />, it can’t be null. But, in the case of a custom input component, null is actually very common I think. <ReactSelect />, for example, can be null.

Input components really just need to follow the contract of value/onChange. They might invoke onChange with an array or an object. You wouldn’t want the initial value of an input that calls onChange with an object to take an empty string as an initial value. Therefore, null makes the most sense.

This is a contrived example, but imagine you have a custom input component that knows how to manage objects with a certain structure.

class DistanceInput extends Component {
  static propTypes = {
    onChange: PropTypes.func.isRequired,
    value: PropTypes.shape({
      distance: PropTypes.number.isRequired
    }),
  };

  handleChange = (event) => {
    const { value } = event.target;

    if (value) {
      this.props.onChange({
        distance: parseInt(event.target.value, 10)
      });
    } else {
      this.props.onChange(null);
    }
  }

  render () {
    return (
      <input
        type='number'
        value={this.props.value ? this.props.value.distance : 999}
        onChange={this.handleChange}
      />
    );
  }
}

Create the form:

class FormState extends MobxReactForm {}

// normally, this would be passed as a prop, but for time saving...
const form = new FormState({
  fields: [{
    name: 'distance',
    label: 'Distance'
  }]
};

class Form extends Component {
  render() {
    const distance = form.$('distance');

    return (
      <form onSubmit={form.submit}>
        <DistanceInput
          value={distance.value}
          onChange={distance.onChange}
        />
      </form>
    );
  }
}

This will trigger a propType error, because the distance input expects null or an object.

Read more comments on GitHub >

github_iconTop Results From Across the Web

NULL and DEFAULT value: MySql behavior does not seem ...
1 Answer 1 ... You are "forcing" to insert null value in a field that can't be null, it has default value, ok,...
Read more >
BIRT » Why can't parameter default values be NULL?
However, I can't seem to pass in null values no matter what. First, I can't set a dataset param default value to null...
Read more >
11.6 Data Type Default Values
If the column cannot take NULL as a value, MySQL defines the column with no explicit DEFAULT clause.
Read more >
Setting default value to NULL values when using Get-ADUser
I have tried a ForEach loop statement but I can't seem to figure this one out. What is not explicitly allowed should be...
Read more >
Dart Null Safety: The Ultimate Guide to Non-Nullable Types
Non-nullable variables must always be initialized with non-null values. To fully understand all the Null Safety features, practice using them with Dartpad. Dart ......
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