Currently the submit button is active and shows an error message when its clicked with no data, but I want to change this so that the submit button is disabled until there is data.
<Button
onClick={submitId}
loading={isLoading}
type="submit"
disabled={}
>
Submit
</Button>
const submitId = async () => {
setIsLoading(true);
const result = await executeId(a, b);
if (result) {
available.custom.push(`${Name}.model`);
setServiceResult(`${Name} created`);
} else {
setServiceResult("failed");
}
setIsLoading(false);
};
const [isLoading, setIsLoading] = useState(false);
2
Answers
Validate the data and pass the result to
disabled
propThe other answer is correct. You need to check in your
disabled
attribute to whether your value is valid.If you are using a
<form>
might want to addrequired
to the input field.Doing that will block submissions until the field has a value (documentation).