【发布时间】:2021-07-29 19:59:32
【问题描述】:
我有一个输入框,如果从数据库中获取任何值并且输入文本框是可编辑的,我会在其中显示值。但是,这个输入框并没有反映按键的变化。
以下是必要的代码:
constructor(props) {
super(props)
const { minorModel } = this.props;
this.state =
{
galleryModel: minorModel.galleryModel,
selectedIndex: 0
}
}
componentDidUpdate() {
this.props.minorModel.galleryModel = this.state.galleryModel;
}
onInputChange=(e)=>{
this.setState({
[e.target.name]:e.target.value
})
}
{this.state.galleryModel.photos.length > 0 &&
<PtInput type="text" label="Comment"
value={this.state.galleryModel.Comments[this.state.selectedIndex] === null ?
"Not Available " :
this.state.galleryModel.Comments[this.state.selectedIndex]}
onChange={this.onInputChange}
name={`${"Comment "+[this.state.selectedIndex]}`}
disabled={this.props.disabled}
/>
}
我可能认为问题是因为我使用的数据模型在构造函数中作为道具传递,并且该数据模型中的值没有改变,因此新值没有反映。是这样吗?非常感谢任何帮助来解决此问题。我还有一个 componentDidUpdate 来反映更改。
【问题讨论】:
-
我没听懂。我将道具模型分配给此组件中的状态变量。现在我可以为
OnInputChangethis.setState({ [this.state.galleryModel.Comments[this.state.selectedIndex]]:e.target.value })做这样的事情吗