There is ONE input element, whose value is given by the user. and 3 buttons, whose values are coming from the states.
So when I put intput value as "11" and press SUBMIT1 button,
then the output in console should be
{userId: "11", submit1: "1"}
Instead I get the whole submit values too
{userId: "11", submit1: "1", submit2: "2", submit3: "3"}
Also, I got a response error (LINE NUMBER 23) : I am trying to send one INPUT value and ONE submit value (of the button that was clicked..)
PostForm.jsx file
import React, { Component } from "react";
import axios from "axios";
class PostForm extends Component {
constructor(props) {
super(props);
this.state = {
userId: "",
submit1: "1",
submit2: "2",
submit3: "3"
};
}
changeHandler = e => {
this.setState({ [e.target.name]: e.target.value });
};
submitHandler = e => {
e.preventDefault();
console.log(this.state);
axios
.post(
"https://jsonplaceholder.typicode.com/posts/${userId}/${submit}",
this.state
)
.then(response => {
console.log(response);
})
.catch(error => {
console.log(error);
});
};
render() {
const { userId, submit1, submit2, submit3 } = this.state;
return (
<div>
<form onSubmit={this.submitHandler}>
<div>
<input
type="text"
name="userId"
value={userId}
onChange={this.changeHandler}
/>
</div>
<button value={submit1} onChange={this.changeHandler} type="submit">
Submit1
</button>
<button value={submit2} onChange={this.changeHandler} type="submit">
Submit2
</button>
<button value={submit3} onChange={this.changeHandler} type="submit">
Submit3
</button>
</form>
</div>
);
}
}
export default PostForm;
