Rather pivot detection has been passed or set by default.

I would be comfortable if one of the two closely related functions were in React, purely for convenience / syntactic sugar, since I know I can do what I want without them, it's just a little uglier / slower. I suspect neither of them can be done, so instead write two separate questions that are nearly identical. I ask both of them in this question so you can ruin my dreams right away :)

I found myself wanting to use the convenience function a few times, a way of storing in props a little more knowledge of what props are set, so as not to write so many helper methods.

To use my last example, I have two props, errorEvaluator and warningEvalutator, which are functions that check that a dataset should be flagged with a warning or error condition. They both have default props that always return false if no score is provided, so I don't have to do null checks all the time in code.

Now I want to know if a warning or an error evaluator was provided, so I won't compensate everything to make room for a warning / error icon that will never be used, I can't just check for undefined since I'm using defaultProps. I would like one of two shortcut options to check if these values ​​were not specified by an operator. So something like:

  • some function that checks, rather, the alert was passed by the user, as opposed to being the default props I could call to see what the value was set.
  • How can I quickly do props modification on component update where I can set showIcon support if both evaluators are undefined and then set the evaluators to my default always false evaporators; without having to resort to lifecycle methods that somehow seem redundant.

Is there any of these, or an equivalent quick way of doing such a check?

The cleanest option I know is to not use the default props and instead have methods that return my evaluators, return the default evaluator if none are passed, when I want to use the evaluator, and then check for the evaluator undefined in props when I want to know if an evaluator has been provided. This works, I'm just wondering if there is any faster or colder syntactic sugar that I am not aware of?

+3


source to share


2 answers


For point 1: you cannot check if it was skipped or set as the default, as this will lose the entire point of the default props. If you want to know if a missed one was passed or not, you shouldn't use the default prop and check for undefined.



Regarding point 2: not only is there no way to do this, but you should never change props from the component that receives them. It's just against the way React works. The component should take the props and process them. If any value or user interface element is the "output" of a component and computed from its props, it should not be the pivot of that component. If you need to detect when props change, then lifecycle methods don't go overboard, they are a surefire way to do it.

+3


source


Since you know which features are standard, you can test them explicitly. You can use equality comparison:

if(this.props.errorEvaluator === this.defaultErrorEvaluator) {
    // Set up styles to exclude formatting
}

      



You will need to digress a little to make sure that you have always used this particular evaluator and that the caller hasn't dealt with it, but it shouldn't be hard.

+1


source







All Articles