gpt4 book ai didi

javascript - React - 无法以编程方式从输入文本元素调度 onChange 事件

转载 作者:行者123 更新时间:2023-11-30 19:56:16 27 4
gpt4 key购买 nike

我有两个组件:ParentComponentChildComponent

ChildComponent 有一个 input[type="text"] 元素,当它更改其文本时,该事件将传播到 ParentComponent通过事件 changeonChange 监听器。

下面的代码是对一个更大问题的简化,这就是为什么您会在此处看到一些突出显示的要求。

我的问题是我需要触发函数内的change事件:handleClick。我做了一些实验,但运气不佳。

这里有您可以试验的代码沙箱(请提供您的方法的分支):

https://codesandbox.io/s/wqw49j5krw

这里有代码:

ParentComponent.js

import React from "react";
import ChildComponent from "./ChildComponent";

export default class ParentComponent extends React.Component {
constructor(props) {
super(props);
this.state = {
name: "Peter"
};
}
handleChange = event => {
let target = event.target;
let value = target.value;
this.setState({
name: value
});
};
render() {
return (
<div>
<ChildComponent value={this.state.name} onChange={this.handleChange} /><br />
<span>Hello</span>&nbsp;
<span>{this.state.name}!</span>
</div>
);
}
}

ChildComponent.js

import React from "react";
import ReactDOM from "react-dom";

export default class ChildComponent extends React.Component {
constructor(props) {
super(props);
this.state = {
value: this.props.value
};
}
handleChange = event => {
const name = event.target.value;
this.setState({ value: name });
if (this.props.onChange !== undefined) {
this.props.onChange(event);
}
};
handleClick = name => {
const inputName = this.refs.name;
console.log('Name before being changed: ' + inputName.value); // this works
// PROBABLY HERE IS WHERE THE CODE NEEDS TO BE MODIFIED
this.setState({ value: name });
var event = new Event('input', { bubbles: true });
inputName.dispatchEvent(event); // this doesn't propagate the event to the parent
};
render() {
return (
<div>
{"Your name: "}
<input type="text"
value={this.state.value}
onChange={this.handleChange}
ref="name"
/>
{/* requirement: the following 2 lines cannot be modified */}
<button onClick={e => { this.handleClick("Steve"); }}>Steve</button>
<button onClick={e => { this.handleClick("Emily"); }}>Emily</button>
</div>
);
}
}

关于如何让它工作的任何想法?

谢谢!

最佳答案

你错过了输入变化的轨迹,

Because React tracks when you set the value property on an input to keep track of the node's value. When you dispatch a change event, it checks it's last value against the current value and if they're the same it does not call any event handlers (as no change has taken place as far as react is concerned). So we have to set the value in a way that React's value setter function will not be called, which is where the setNativeValue comes into play.

在这里您设置状态而不是直接更改输入的值,因此在您调度事件时它不会获得更新的值。如果你像 input.value 这样直接写值,它就无法跟踪输入的变化。

因此,您应该设置值并发送事件,这样您可以在发送事件时获得更新后的值。

这是 reference 的链接和 another ,还有其他方法可以触发更改事件。

这是设置属性所需的函数,以便 React 可以跟踪更改,

  handleClick = name => {
const inputName = this.refs.name;
console.log("Name before being changed: " + inputName.value); // this works

var event = new Event("input", { bubbles: true });

this.setNativeValue(inputName, name);
inputName.dispatchEvent(event);
};

setNativeValue = (element, value) => {
const valueSetter = Object.getOwnPropertyDescriptor(element, "value").set;
const prototype = Object.getPrototypeOf(element);
const prototypeValueSetter = Object.getOwnPropertyDescriptor(
prototype,
"value"
).set;

if (valueSetter && valueSetter !== prototypeValueSetter) {
prototypeValueSetter.call(element, value);
} else {
valueSetter.call(element, value);
}
};

Demo

关于javascript - React - 无法以编程方式从输入文本元素调度 onChange 事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53954029/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com