如何在ReactJS中将属性列表作为字符串注入

问题描述 投票:0回答:2

我正在编写一个React组件,使我的文本可编辑。单击时,它会呈现输入,当模糊时,它会像以前一样呈现文本。

为了使它工作,我必须在我的元素中加入很多属性。我想处理许多类型的输入(文本,数字,日期,文本区域......),我不想一直重复这个列表。

有没有办法只写一次这个列表并将其注入我的元素?

像这样的东西:

const options = "value={this.props.value} placeholder={this.props.placeholder} onChange={this.props.onChange}  onBlur={this.stopEdit} ref={(e) => {this.eRef = e;}}";

后来这样使用:

render() {
    return (<input type="text" {{options}} />);
}

我读过有关dangerouslySetInnerHTML的内容,但这并不是我所需要的。

非常感谢您的帮助!

javascript html reactjs
2个回答
2
投票

为什么你不这样使用

const options = {
  value: this.props.value,
  placeholder: this.props.placeholder,
  ...
}

html = <input type="text" {...options}/>

或者你可以使用

html = React.createElement('input', {type: 'text', ...options})

0
投票

如果您正在查看如何使元素中的文本可编辑,可以将content-editable="true"属性附加到元素。

希望这可以帮助!

© www.soinside.com 2019 - 2024. All rights reserved.