我需要将我的jquery应用程序完全转换为React,并且不知道如何获取,在React中设置任何属性。我是用jquery做的,但不知道怎么用反应来启动它。看了一些例子,但没有找到适合我问题的解决方案。
class App extends React.Component {
changeAttr() {
$('[data-correctans]').attr('data-correctans','chnaged_status');
}
render () {
return (
<div>
<div data-correctans="staus">Hi I am new to react, Need to do some DOM manipulation!!</div>
<button onClick={() => this.changeAttr()}>Change attr!</button>
</div>
);
}
}
ReactDOM.render(<App />, document.getElementById('root'));
答案 0 :(得分:0)
您需要更改自己对网络应用的看法。 React.Component
只对状态和道具作出反应。 React非常容易学习,所以阅读官方教程可能会对你有所帮助(基本部分花费不到几个小时)。
changeAttr() {
// $('[data-correctans]').attr('data-correctans','chnaged_status');
this.setState({ data_correctans: 'changed_status' });
}
render () {
// the followings are JSX, not the HTML, so you can use JS code within { }
return (
<div>
<div data-correctans="{this.state.data_correctans}">Hi I am new to react, Need to do some DOM manipulation!!</div>
<button onClick={() => this.changeAttr()}>Change attr!</button>
</div>
);
}