[英]How to link a component from one file to a component in another file?
Say I have a component (header.jsx) which has a button. 说我有一个带有按钮的组件(header.jsx)。 When that button is clicked I want to open a Dialog that I created with Material UI. 单击该按钮后,我想打开一个用Material UI创建的对话框。 This dialog component is in another file I called dialog.jsx. 该对话框组件位于另一个名为dialog.jsx的文件中。 I know how I could do it all in one file: simply create a function that is linked to the button and calls a show event on the dialog tag. 我知道如何在一个文件中完成所有操作:只需创建一个链接到按钮并在dialog标记上调用show事件的函数。 However if I were to separate these two into components in separate files, how can I accomplish this? 但是,如果我将这两部分分成单独的文件中的组件,该如何完成呢?
you can user ReactDom.render
method for rendering a component on a particular action 您可以使用ReactDom.render
方法在特定动作上渲染组件
suppose I have dialog.jsx like 假设我有dialog.jsx
var Dialog=React.createClass({
//render function goes here
});
module.exports=Dialog
I want to use this component on button click then your parent component should be like 我想在单击按钮时使用此组件,然后您的父组件应该像
var parent=React.createClass({
click:function()
{
ReactDom.render(<Dialog />,document.getElementById("child");
},
render:function()
{
return(
<div>
<div id="child">
<div>
<button onClick={this.click} />
</div>
)
}
});
hope this may help you 希望这对您有帮助
Do it like this.... 像这样做....
var React = import('react'),
Dialog = require("path.to.dialog.jsx"),
header = React.createClass({
componentWillReceiveProps: function(){
this.setState({"showDialog" : false});
},
onButtonClick : function(){
this.setState({"showDialog" : true});
},
render: function(){
return (
<div>
<Your other layout>
<Button text="Open dialog" click={this.onButtonClick} />
</Your other layout>
<Dialog show={this.state.showDialog} />
</div>
);
}
});
You can accomplish what you're trying to do by passing a ref
prop to the Dialog
component. 您可以通过将ref
prop传递给Dialog
组件来完成您要尝试的操作。 You then can call methods on the Dialog
component by reference to the this.refs
object inside the Parent
component. 然后,您可以通过引用Parent
组件内部的this.refs
对象来调用Dialog
组件上的方法。 Here's a CodePen showing this in action. 这是一个CodePen展示了它的作用。 http://codepen.io/anon/pen/pgNVpa Obviously, the code in CodePen is all in one file. http://codepen.io/anon/pen/pgNVpa显然,CodePen中的代码都在一个文件中。 Below is how you would do it in multiple files. 下面是如何在多个文件中执行操作。
Now, just because you can do this doesn't mean you should. 现在,仅因为您可以执行此操作并不意味着您应该这样做。 I would recommend the approach @HaZardouS took in his answer. 我建议@HaZardouS接受他的回答。 That's the classic React pattern. 那就是经典的React模式。 In the React docs, they caution against overuse of refs
. 在React文档中,他们警告不要过度使用refs
。
https://facebook.github.io/react/docs/more-about-refs.html https://facebook.github.io/react/docs/more-about-refs.html
If you have not programmed several apps with React, your first inclination is usually going to be to try to use refs to "make things happen" in your app. 如果您尚未使用React编写多个应用程序,那么您的第一个倾向通常是尝试使用引用在应用程序中“使事情发生”。 If this is the case, take a moment and think more critically about where state should be owned in the component hierarchy. 如果是这种情况,请花点时间仔细考虑一下在组件层次结构中应在何处拥有状态。 Often, it becomes clear that the proper place to "own" that state is at a higher level in the hierarchy. 通常,很明显,“拥有”该状态的适当位置在层次结构中处于较高级别。 Placing the state there often eliminates any desire to use refs to "make things happen" – instead, the data flow will usually accomplish your goal. 将状态放在那里通常消除了使用引用“使事情成真”的任何愿望–相反,数据流通常可以实现您的目标。
Dialog.jsx Dialog.jsx
class Dialog extends React.Component {
constructor(props, context) {
super(props, context);
this.state = {
open: false
};
this.open = this.open.bind(this);
this.close = this.close.bind(this);
}
open() {
this.setState({
open: true
});
}
close() {
this.setState({
open: false
});
}
render() {
if (this.state.open) return <p>Dialog is open.</p>;
else return <p>Dialog is closed.</p>
}
}
export default Dialog;
Parent.jsx Parent.jsx
import Dialog from './Dialog';
class Parent extends React.Component {
constructor(props, context) {
super(props, context);
this.openDialog = this.openDialog.bind(this);
this.closeDialog = this.closeDialog.bind(this);
}
openDialog() {
this.refs.dialog.open();
}
closeDialog() {
this.refs.dialog.close();
}
render() {
return (
<div>
<button onClick={this.openDialog}>Open Dialog</button>
<button onClick={this.closeDialog}>Close Dialog</button>
<Dialog ref="dialog" />
</div>
);
}
}
React.render(<Parent />, document.querySelector('#react-app'));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.