admin管理员组文章数量:1336632
My goal is to implement a React controlled input using the useReducer
hook.
Inside the reducer I need the event.target.value
and event.target.selectionStart
to get the current value and caret position. So I've thought about sending the event
as the payload
for the ON_CHANGE
action.
This is what I'm trying:
function reducer(state = "", action) {
console.log("From reducer... action.type: " + action.type);
switch (action.type) {
case "ON_CHANGE": {
const event = action.payload;
const caretPosition = event.target.selectionStart;
const newValue = event.target.value;
console.log("From reducer... event.target.value: " + event.target.value);
console.log(
"From reducer... event.target.selectionStart: " + caretPosition
);
return newValue;
}
default: {
return state;
}
}
}
export default function App() {
console.log("Rendering App...");
const [state, dispatch] = useReducer(reducer, "");
return (
<div className="App">
<input
value={state}
onChange={event => dispatch({ type: "ON_CHANGE", payload: event })}
/>
</div>
);
}
It works for the 1st letter typed, but it breaks on the 2nd.
This is the error:
Warning: This synthetic event is reused for performance reasons. If you're seeing this, you're accessing the property
target
on a released/nullified synthetic event. This is set to null. If you must keep the original synthetic event around, use event.persist(). Seereact-event-pooling
for more information.
What should I do? Where do I need to call event.persist()
. Should I do it on the reducer
or I need to do it before sending it as a parameter, inside the onChange()
handler.
Or is it better to send only those properties, instead of sending the full event
object?
Like:
onChange={ event =>
dispatch({
type: "ON_CHANGE",
payload: {
value: event.target.value,
caretPosition: event.target.selectionStart
}
})
}
My goal is to implement a React controlled input using the useReducer
hook.
Inside the reducer I need the event.target.value
and event.target.selectionStart
to get the current value and caret position. So I've thought about sending the event
as the payload
for the ON_CHANGE
action.
This is what I'm trying:
https://codesandbox.io/s/optimistic-edison-xypvn
function reducer(state = "", action) {
console.log("From reducer... action.type: " + action.type);
switch (action.type) {
case "ON_CHANGE": {
const event = action.payload;
const caretPosition = event.target.selectionStart;
const newValue = event.target.value;
console.log("From reducer... event.target.value: " + event.target.value);
console.log(
"From reducer... event.target.selectionStart: " + caretPosition
);
return newValue;
}
default: {
return state;
}
}
}
export default function App() {
console.log("Rendering App...");
const [state, dispatch] = useReducer(reducer, "");
return (
<div className="App">
<input
value={state}
onChange={event => dispatch({ type: "ON_CHANGE", payload: event })}
/>
</div>
);
}
It works for the 1st letter typed, but it breaks on the 2nd.
This is the error:
Warning: This synthetic event is reused for performance reasons. If you're seeing this, you're accessing the property
target
on a released/nullified synthetic event. This is set to null. If you must keep the original synthetic event around, use event.persist(). Seereact-event-pooling
for more information.
What should I do? Where do I need to call event.persist()
. Should I do it on the reducer
or I need to do it before sending it as a parameter, inside the onChange()
handler.
Or is it better to send only those properties, instead of sending the full event
object?
Like:
onChange={ event =>
dispatch({
type: "ON_CHANGE",
payload: {
value: event.target.value,
caretPosition: event.target.selectionStart
}
})
}
Share
Improve this question
asked May 16, 2020 at 10:26
cbdevelopercbdeveloper
31.5k44 gold badges200 silver badges396 bronze badges
1 Answer
Reset to default 7Just pass the event.target.value
instead, because like the error says, synthetic events don't persist.
function reducer(state = "", action) {
switch (action.type) {
case "ON_CHANGE": {
const newValue = action.payload;
return newValue;
}
default: {
return state;
}
}
}
export default function App() {
const [state, dispatch] = useReducer(reducer, "");
return (
<div className="App">
<input
value={state}
onChange={event =>
dispatch({ type: "ON_CHANGE", payload: event.target.value })
}
/>
</div>
);
}
本文标签: javascriptHow to implement a react controlled input using useReducerStack Overflow
版权声明:本文标题:javascript - How to implement a react controlled input using useReducer? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742336926a2455809.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论