【发布时间】:2015-08-10 09:45:39
【问题描述】:
我在页面中包含了两个 JS 文件,分别是 utility.js 和 utility1.js
utility.js 的代码
var HelloWorld = React.createClass({
render: function() {
return (
<p>
Hello, <input type="text" ref="mytestinput" placeholder="Your name here" />!<br />
It is {this.props.date.toTimeString()}
</p>
);
}
});
setInterval(function() {
React.render(
<HelloWorld date={new Date()} />,
document.getElementById('container')
);
}, 1000);
utility1.js 的代码
var MyComponent = React.createClass({
handleClick: function() {
// Explicitly focus the text input using the raw DOM API.
React.findDOMNode(HelloWorld.refs.mytestinput).focus();
},
render: function() {
// The ref attribute adds a reference to the component to
// this.refs when the component is mounted.
return (
<div>
<input type="text" ref="myTextInput" />
<input
type="button"
value="Focus the text input"
onClick={this.handleClick}
/>
</div>
);
}
});
React.render(
<MyComponent />,
document.getElementById('container1')
);
这里的问题是我想关注来自utility1.js 的utility.js 的HelloWorld 组件的输入。我看到它们是安装组件的一种方法 findDOMNode 。但是这段代码对我不起作用。有人可以试试这个 JS Fiddle here 并让我知道可能的解决方案。
【问题讨论】:
标签: javascript validation input components reactjs