如何将事件传递给 setState 回调函数?
How can I pass event to setState callback function?
React中是否可以将外部事件传递给setState的回调函数?
示例
someFunc(event) {
this.setState(
{
value: event.target.value
},
() => {
this.props.onChange(event); // <- cannot pass to here
}
);
}
编辑:请参阅下面 Liam 接受的解决方案以获得出色的答案,
这是我的问题的具体解决方案:
解决方案
someFunc(event) {
event.persist() // <- add this line and event should pass without a problem
this.setState(
{
value: event.target.value
},
() => {
this.props.onChange(event);
}
);
}
编辑:错误的解决方案
一个解决方案是编写另一个函数的引用,例如
updateInput(e) {
this.setState(
{
value: e.target.value
},
this.handleUpdate(e)
);
}
handleUpdate(e) {
this.props.onChange(e, this.props.tag);
}
编辑:
正如 Felix King 在下面的评论中所指出的,这根本不是解决方案。在设置状态之前它将 运行 handleUpdate
函数,这违背了目的。真正的解决方案由 Liam 在上面发布。
您需要提取值或使用 e.persist()
https://reactjs.org/docs/events.html#event-pooling
class App extends React.Component {
something = (value) =>{
{console.log(value, 'coming from child')}
}
render() {
return (
<div >
<Hello text={this.something} />
</div>
);
}
}
class Hello extends React.Component {
constructor() {
super()
this.state = {
value: ''
}
}
onChange = (e) => {
const value = e.target.value;
this.setState({ value }, () => {
this.props.text(value)
})
}
render() {
return (
<div style={{ padding: 24 }}>
<input onChange={this.onChange} value={this.state.value} />
</div>
);
}
}
ReactDOM.render(<App />, document.getElementById('root'));
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>
<div id='root' ></div>
或者如果您打算传递值状态,您可以在回调中使用 this.state.value 它会起作用。
someFunc(event) {
this.setState(
{
value: event.target.value
},
() => {
this.props.onChange(this.state.value); // <- this should work
}
);
}
React中是否可以将外部事件传递给setState的回调函数?
示例
someFunc(event) {
this.setState(
{
value: event.target.value
},
() => {
this.props.onChange(event); // <- cannot pass to here
}
);
}
编辑:请参阅下面 Liam 接受的解决方案以获得出色的答案, 这是我的问题的具体解决方案:
解决方案
someFunc(event) {
event.persist() // <- add this line and event should pass without a problem
this.setState(
{
value: event.target.value
},
() => {
this.props.onChange(event);
}
);
}
编辑:错误的解决方案
一个解决方案是编写另一个函数的引用,例如
updateInput(e) {
this.setState(
{
value: e.target.value
},
this.handleUpdate(e)
);
}
handleUpdate(e) {
this.props.onChange(e, this.props.tag);
}
编辑:
正如 Felix King 在下面的评论中所指出的,这根本不是解决方案。在设置状态之前它将 运行 handleUpdate
函数,这违背了目的。真正的解决方案由 Liam 在上面发布。
您需要提取值或使用 e.persist()
https://reactjs.org/docs/events.html#event-pooling
class App extends React.Component {
something = (value) =>{
{console.log(value, 'coming from child')}
}
render() {
return (
<div >
<Hello text={this.something} />
</div>
);
}
}
class Hello extends React.Component {
constructor() {
super()
this.state = {
value: ''
}
}
onChange = (e) => {
const value = e.target.value;
this.setState({ value }, () => {
this.props.text(value)
})
}
render() {
return (
<div style={{ padding: 24 }}>
<input onChange={this.onChange} value={this.state.value} />
</div>
);
}
}
ReactDOM.render(<App />, document.getElementById('root'));
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>
<div id='root' ></div>
或者如果您打算传递值状态,您可以在回调中使用 this.state.value 它会起作用。
someFunc(event) {
this.setState(
{
value: event.target.value
},
() => {
this.props.onChange(this.state.value); // <- this should work
}
);
}