今天在使用antd
的input
组件和mobx
做一个很简单的功能,获取input中的输入内容,然后传给后端做查询,可偏偏就遇到奇奇怪怪的坑~
删减后的代码大概长这样
import store from './store'
class SearchForm extends Component {
handleSearchInput = e => {
console.log(e.target.value)
}
render() {
return(
<Input.Search
placeholder="支持标题、内容模糊查询"
value={store.keyword}
enterButton
onChange={this.handleSearchInput}
/>
)
}
}
可以说是非常非常简单常见的需求了,在输入的事件中获取输入框的内容。可是当我输入内容时,控制台报错了:
W
a
r
n
i
n
g
:
T
h
i
s
s
y
n
t
h
e
t
i
c
e
v
e
n
t
i
s
r
e
u
s
e
d
f
o
r
p
e
r
f
o
r
m
a
n
c
e
r
e
a
s
o
n
s
.
I
f
y
o
u
′
r
e
s
e
e
i
n
g
t
h
i
s
,
y
o
u
′
r
e
a
c
c
e
s
s
i
n
g
t
h
e
p
r
o
p
e
r
t
y
‘
t
a
r
g
e
t
‘
o
n
a
r
e
l
e
a
s
e
d
/
n
u
l
l
i
f
i
e
d
s
y
n
t
h
e
t
i
c
e
v
e
n
t
.
T
h
i
s
i
s
s
e
t
t
o
n
u
l
l
.
I
f
y
o
u
m
u
s
t
k
e
e
p
t
h
e
o
r
i
g
i
n
a
l
s
y
n
t
h
e
t
i
c
e
v
e
n
t
a
r
o
u
n
d
,
u
s
e
e
v
e
n
t
.
p
e
r
s
i
s
t
(
)
.
S
e
e
h
t
t
p
s
:
/
/
f
b
.
m
e
/
r
e
a
c
t
−
e
v
e
n
t
−
p
o
o
l
i
n
g
f
o
r
m
o
r
e
i
n
f
o
r
m
a
t
i
o
n
.
\color{red}{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(). See https://fb.me/react-event-pooling for more information.}
Warning:Thissyntheticeventisreusedforperformancereasons.Ifyou′reseeingthis,you′reaccessingtheproperty‘target‘onareleased/nullifiedsyntheticevent.Thisissettonull.Ifyoumustkeeptheoriginalsyntheticeventaround,useevent.persist().Seehttps://fb.me/react−event−poolingformoreinformation.
此时e.target的值也为null
这就让我有点懵了,在仔细阅读报错信息后找到了一丝线索use event.persist()
(所以说遇到报错不要慌,冷静分析报错信息~~)
然后去网上搜素了一下资料,发现原来官网上就有说明~~
注意:
如果你想异步访问事件属性,你需在事件上调用 event.persist(),此方法会从池中移除合成事件,允许用户代码保留对事件的引用。
加上方法后的代码
import store from './store'
class SearchForm extends Component {
handleSearchInput = e => {
e.persist()
console.log(e.target.value)
}
render() {
return(
<Input.Search
placeholder="支持标题、内容模糊查询"
value={store.keyword}
enterButton
onChange={this.handleSearchInput}
/>
)
}
}
完事~