I am using a <button>
in a React component that has onClick
and disabled
attributes. The onButtonClick
action and isButtonDisabled
variable are retrieved from the store (I use Zustand). The isButtonDisabled
selector includes some logic to determine whether the button should be disabled or not. My expectation is that the React component will disable the button using this variable, and only call onClick when the button is not disabled. However, it's possible that the component may not disable the button and call onButtonClick
even when the button should be disabled.
This raises the question of whether I should run the same logic in the onButtonClick
action and check if the button should be disabled. If the button is disabled, should I simply return early and do nothing?
This would be useful in cases where we forget to set the disabled
attribute on a button and execute that action. Do I need to run the same logic that calculates whether the button should be disabled again in the action, or is it enough to trust the component to set disabled and use isButtonDisabled
on a button?
export const Component: React.FC = () => {
// Zustand store, it calls a selector that runs some logic.
const isBtnDisabled = useAppState(`isBtnDisabled`);
const { onBtnClick } = useActions();
return (
<Button onClick={onBtnClick} isDisabled={isBtnDisabled}>
Delete Layer
</Button>
);
}