Created
October 2, 2018 07:15
-
-
Save TimeBandit/f6c5a5809d0fc11458196a69d7475cd6 to your computer and use it in GitHub Desktop.
using string return values to signal errors #patterns #js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// reurn a string message to signal an error | |
handleAddOption(option) { | |
if (!option) { | |
return "Enter valid value"; | |
} else if (this.state.options.indexOf(option) > -1) { | |
return "This option already exists"; | |
} else { | |
this.setState((prevState, props) => { | |
return { | |
options: [...prevState.options, option] | |
}; | |
}); | |
} | |
} | |
// elsewhere check the return value; if it's a string | |
// signal error | |
const error = handleOption(option); | |
if (error){ | |
// do something | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment