Skip to content

Instantly share code, notes, and snippets.

@bensampaio
Created June 18, 2020 14:56
Show Gist options
  • Save bensampaio/17bdc5ceb0af4108053ada570eb5a2d7 to your computer and use it in GitHub Desktop.
Save bensampaio/17bdc5ceb0af4108053ada570eb5a2d7 to your computer and use it in GitHub Desktop.
Interview questions
// How to optimize this component?
class DangerButton extends PureComponent {
handleClick() {
throw new Error('I warned you!');
}
render() {
return (
<button onClick={this.handleClick.bind(this)}>
{"Don't press me!"}
</button>
);
}
}
// How to optimize this function?
function incrementAll(list, options) {
return list.map((value) => {
value += options.add;
value *= options.multiply;
return value;
});
}
// How to optimize this component?
const InputField = ({ name, pattern, value }) => {
const [currentValue, setValue] = useState(value);
const handleChange = useCallback((event) => {
setValue(event.target.value);
});
return (
<>
<input name={name} value={currentValue} onChange={handleChange} />
{currentValue && !pattern.test(currentValue) && (
<div>
This is wrong, please change it.
</div>
)}
</>
);
}
// How to optimize this component?
const LuckyButton = ({ value }) => ({
<button onClick={() => alert(`Your lucky number is ${value}`)}>
Press me!
</button>
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment