Expected
e.target.value returns me the value of the input in the form I submitted
Results
e contains a strange React changed object, SyntheticEvents?
return (
<div>
<form onSubmit={ onSubmitName }>
<input id="name_field"
title="Name:"
placeholder={ user.name }/>
</form>
</div>
)
^ Above is my form, below is the onSubmit which is inside of a Redux mapDispatchToProps
const mapDispatchToProps = (dispatch) => {
return {
onSubmitName: (e) => {
console.log('e', e);
e.preventDefault();
const name = document.getElementById('name_field').value;
dispatch({ type: "CHANGE_NAME", payload: handleOnSubmit(name) })
}
}
}
e.preventDefault no longer exists on the new converted event.
How is this now handled in React? The documentation provides no work arounds. To get e.preventDefault() to work again and e.target.value
Full code
import React from "react"
import { connect } from "react-redux"
const handleOnSubmit = (e) => {
e.preventDefault();
const name = document.getElementById('name_field').value;
return name;
}
const mapDispatchToProps = (dispatch) => {
return {
onSubmitName: (e) => {
console.log('e', e);
e.preventDefault();
const name = document.getElementById('name_field').value;
dispatch({ type: "CHANGE_NAME", payload: handleOnSubmit(name) })
}
}
}
const NameField = ({ user, onSubmitName }) => {
return (
<div>
<form onSubmit={ onSubmitName }>
<input id="name_field"
title="Name:"
placeholder={ user.name }/>
</form>
</div>
)
}
const NameContainer = connect(
mapDispatchToProps
)(NameField);
export default NameContainer;
var nameseems to be a string when you pass it intohandleOnSubmit()indispatch({ type: "CHANGE_NAME", payload: handleOnSubmit(name) }), so theconst handleOnSubmitfunction is accepting a string (not a SyntheticEvent), which does not have apreventDefault()method available - therobinkimvalueprop on input field. - Umair Sarfraz