javascript - 没有调用 Office-fabric-react 文本字段的 onChange
问题描述
onChange
因为我的组件没有触发:
<TextField
label = "City"
required = {true}
placeholder = "Location"
value = {this.props.location && this.props.location.Title ? this.props.location.Title : ""}
onChange = {this._onChangeTitle.bind(this)}
onChanged = {(newValue: string) => {
console.log("onChanged | newValue: ", newValue);
}}
private _onChangeTitle(event: React.FormEvent<HTMLInputElement | HTMLTextAreaElement>, newValue: string): void {
console.log("AddRemoveComponent | _onChangeTitle | newValue: ", newValue);
this._location.Title = newValue;
console.log("AddRemoveComponent | _onChangeTitle | this._location.Title: ", this._location.Title);
}
将onChanged
记录新值,但_onChangeTitle
永远不会被调用。
有什么建议么?
解决方案
在最新版本的事件应该按预期触发,这里有一个演示供您参考。office-ui-fabric-react
TextField.change
避免使用箭头函数并在render
方法中绑定(有关更多详细信息,请参见此处),因为它可能会影响性能,而一种选择是手动绑定构造函数中的方法:
export class TextFieldBasicExample extends React.Component<any, any> {
private location: {Title:string};
constructor(props: {}) {
super(props);
this.location = {Title:""};
this.onChange = this.onChange.bind(this);
this.onChanged = this.onChanged.bind(this);
}
public render(): JSX.Element {
return (
<div>
<TextField
onChange = {this.onChange}
onChanged={this.onChanged} />
</div>
);
}
private onChange(event: React.FormEvent<HTMLInputElement | HTMLTextAreaElement>, newValue: string): void {
this.location.Title = newValue;
}
private onChanged(newValue: string): void {
this.location.Title = newValue;
}
}
推荐阅读
- reactjs - React 在 componentDidUpdate 中获取新数据
- opencv - 如何刷新imshow的显示窗口?
- passport.js - passport-linkedin-oauth2 升级到 Linkedin API v2,范围内包含 r_liteprofile
- python - Python Pandas 数据透视表计算
- firebase - 如何在flutter中使用firebase电话身份验证中的forceResendingToken重新发送otp
- html - 如何在按钮上分离双悬停?
- android - 自定义 appbar / tab 布局以在 Fragment 之间切换
- c - valgrind 地址在分配大小为 4 的块后为 0 字节
- teradata - teradata 的日期移动总和
- opentracing - 开放式跟踪的轻步集成