我想在React.createElement()
函数内获取函数的当前节点。
return React.createClass({
getInitialState: function() {
return ({
expand: false
});
},
expandLaunch: function() {
this.setState({
expand: true
});
// want to do something like
// $('.text').css('display','block');
},
render: function() {
var titlebar = React.createElement(
'div',
{className: 'titlebar', onClick: this.expandLaunch},
'Launcher'
);
//........
var text = React.createElement(
'div',
{className: 'text'},
textarea
);
return React.createElement(
'div',
{className: 'box-wrapper'},
titlebar,
text
);
}
});
因此,通过
expandLaunch()
函数,我想定位元素,以便可以操纵CSS和其他可能的功能。仅供参考,我不使用JSX,而只是普通的jQuery。 最佳答案
使用ref
。
return React.createClass({
getInitialState: function() {
return ({
expand: false
});
},
expandLaunch: function() {
this.setState({
expand: true
});
$(this.refs.text).css('display','block');
},
render: function() {
var titlebar = React.createElement(
'div',
{className: 'titlebar', onClick: this.expandLaunch},
'Launcher'
);
//........
var text = React.createElement(
'div',
{className: 'text', ref: 'text'},
textarea
);
return React.createElement(
'div',
{className: 'box-wrapper'},
titlebar,
text
);
}
});
我确实敦促您使用React提供的工具,通过动态
className
或内联样式来实现此目的,而不是使用jquery,在许多方面都应将其视为与React正交。