I'm trying to figure out how I can set a disabled field in a stateless component that only uses prop. The stateless component has a input field that if empty I would like to disable the submit button within the same component. I was able to update the parent's state via a prop to the child and so wanted to keep it with no state but starting to think I may need it for checking if the button can be enabled or not.
I've tried different methods using refs etc. Here is a codesandbox project I have for an example
https://codesandbox.io/s/840kkk03l
The stateless prop is :
const Childprop = function(props) {
function handleNameChange(e) {
e.preventDefault();
props.onNameChange(e.target.newName.value);
}
function checkDisabled() {
var input = document.getElementById("input");
if (input.value === "") {
return true;
} else {
return false;
}
}
return (
<p>
The logged in user is: {props.nameProp}
<form onSubmit={handleNameChange}>
<input name="newName" type="text" id="input" />
<input type="submit" disabled={checkDisabled} />
</form>
</p>
);
};
Thank you