I'm building a form in React and Redux. The input for wins and losses changes the number input into a string when I type it into the form. The form isn't saving because the data type is supposed to be a number. I don't want the number to change into a string when I input it into the form.
I think the issue might be with my handleOnChange for the form.
class TeamForm extends Component {
handleOnChange = event => {
const { name, value } = event.target;
const currentTeamFormData = Object.assign({},
this.props.teamFormData, {
[name]: value
});
this.props.updateTeamFormData(currentTeamFormData);
};
handleOnSubmit = event => {
event.preventDefault();
this.props.createTeam(this.props.teamFormData);
};
render() {
const { name, wins, losses, logo_url } = this.props.teamFormData;
return (
<div className="teamForm">
<h1>Add a team to the League</h1>
<form onSubmit={this.handleOnSubmit}>
<div>
<label htmlFor="name">Name:</label>
<input
type="text"
onChange={this.handleOnChange}
name="name"
value={name}
/>
</div>
<div>
<label htmlFor="wins">Wins:</label>
<input
type="number"
onChange={this.handleOnChange}
name="wins"
value={wins}
/>
</div>
<div>
<label htmlFor="losses">Losses:</label>
<input
type="number"
onChange={this.handleOnChange}
name="losses"
value={losses}
/>
</div>
<div>
<label htmlFor="logo_url">Logo url:</label>
<input
type="text"
onChange={this.handleOnChange}
name="logo_url"
value={logo_url}
/>
</div>
<button type="submit">Add Team</button>
</form>
</div>
);
}
}
Below is the action creator and reducer associated with updating the form.
export const updateTeamFormData = teamFormData => {
debugger;
return {
type: "UPDATED_DATA",
teamFormData
};
};
export const resetTeamForm = () => {
return {
type: "RESET_TEAM_FORM"
};
};
const initialState = {
name: "",
wins: 0,
losses: 0,
logo_url: ""
};
export default (state = initialState, action) => {
switch (action.type) {
case "UPDATED_DATA":
return action.teamFormData;
case "RESET_TEAM_FORM":
return initialState;
default:
return state;
}
};
Any help or insight is appreciated. Thanks!