I am trying to apply the following form for submission in React, but nothing is being shown in the input text field that I type. Hence the onClick does not get fired as well.
I saw a lot of posts but I could not find the solution to my issue, I'm a beginner as well.
<form>
<input
value={input}
onChange={(e) => setInput(e.target.value)}
type="text"
placeholder="Type a message"
/>
<button onClick={sendMessage} type="submit">
Send
</button>
{/* onClick={sendMessage} */}
</form>
And the sendMessage is defined as below:
function Chat({ messages }) {
const [input, setInput] = useState("");
const sendMessage = async (e) => {
e.preventDefault();
await axios.post("/messages/new", {
message: input,
name: "Abhi",
received: true,
timestamp: "Just Now",
});
setInput("");
};
// below contains the render return () which consists the form shown above.