Answer the question
In order to leave comments, you need to log in
How to handle functional component error in React 16.8?
The component takes a string in props and tries to convert it into a regular expression.
function ControlString(props) {
const regExp = new RegExp(props.template, "g");
...
}
Answer the question
In order to leave comments, you need to log in
The error is still easier to handle not in the child, but in the parent.
The solution came down to this:
const [input, setInput] = useState("");
const [expression, setExpression] = useState("");
const updateInput = e => {
setInput(e.target.value);
try {
new RegExp(e.target.value, "g");
setExpression(e.target.value);
} catch (e) {
setExpression(".^");
}
};
return (
<input onChange={updateInput} />
<ControlString template={expression} />
);
Generally, there are fuses . But you can handle the error when calling the RegExp constructor via try-catch,
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question