Reactjs React事件处理程序上的控制台日志语句导致合成事件警告
代码沙箱: 这是一些非常直接的代码:Reactjs React事件处理程序上的控制台日志语句导致合成事件警告,reactjs,Reactjs,代码沙箱: 这是一些非常直接的代码: const Form = ({ form, updateForm }) => { const handleChange = (event, value) => { console.log(event, value); console.log(event.target.name, event.target.value); const newForm = { ...form, ...{ [event.target.nam
const Form = ({ form, updateForm }) => {
const handleChange = (event, value) => {
console.log(event, value);
console.log(event.target.name, event.target.value);
const newForm = { ...form, ...{ [event.target.name]: event.target.value } };
updateForm(newForm);
};
return (
<form>
<input
name="value1"
value={form.value1}
onChange={event => handleChange(event)}
/>
</form>
);
};
const Form1 = connect(
state => ({ form: state.form1 }),
dispatch => ({ updateForm: newForm => dispatch(updateFormOne(newForm)) })
)(Form);
function Home() {
return (
<div>
<h2>You're trying to console.log()
an asynchronous synthetic event that is removed by the time the callback
is executed. If you wish to to persist the event, then use event.persist()
.
Using event.persist()
, you can see all of the event
properties:
ispatchConfig: Object
_targetInst: FiberNode
nativeEvent: InputEvent
type: "change"
target: <input name="value1" value="this is form a1"></input>
currentTarget: null
eventPhase: 3
bubbles: true
cancelable: false
timeStamp: 2926.915000000008
defaultPrevented: false
isTrusted: true
isDefaultPrevented: function () {}
isPropagationStopped: function () {}
_dispatchListeners: null
_dispatchInstances: null
isPersistent: function () {}
<constructor>: "SyntheticEvent"
constform=({Form,updateForm})=>{
常量handleChange=(事件、值)=>{
console.log(事件、值);
日志(event.target.name、event.target.value);
const newForm={……form,{[event.target.name]:event.target.value};
updateForm(newForm);
};
返回(
handleChange(事件)}
/>
);
};
const Form1=连接(
state=>({form:state.form1}),
dispatch=>({updateForm:newForm=>dispatch(updateFormOne(newForm))})
)(表格);
函数Home(){
返回(
您正在尝试console.log()
执行回调时删除的异步合成事件。如果希望持久化事件,请使用event.persist()
使用event.persist()
,您可以看到所有事件的属性:
const handleChange = ({ target: { value, name } }) => {
console.log(name, value);
const newForm = { ...form, [name]: value };
updateForm(newForm);
};