简体   繁体   English

如何将一个文件中的组件链接到另一个文件中的组件?

[英]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.

相关问题 从另一个文件中的另一个组件更改一个组件的 state - Changing state of one component from another component in another file 如何将输入的数据从一个html文件移动到其ts文件,然后再移动到另一个组件文件? - How to move the entered data from one html file to its ts file and then to another component file? 如何从组件中获取变量并在另一个组件或文件中使用它 React - How to take variable from component and use it in another component or file React 为什么一个组件的css文件与ReactJS中另一个组件的css文件交互? 如何处理? - Why css file of one component interacted with the css file of another component in ReactJS? How to handle it? 如何将一个文件组件的状态 (useState) 发送到另一个文件的组件? - How can I send the state (useState) of one file component to another file's component? React / Redux:如何将此状态从一个组件传递到另一个文件? - React/Redux: How can I pass this state from one component to another file? React - 如何将 API 数据从一个组件传递到另一个 js 文件中的另一个组件? - React - How can I pass API data from one component to another in a different js file? 如何从另一个单文件组件VueJS触发单文件组件中的方法 - How to trigger a method in Single File Component from another Single File Component VueJS 如何从另一个js文件调用vue组件方法 - How to call a vue component method from another js file ReactJS如何从同一文件上的另一个函数调用组件函数? - ReactJS how to call a component function from another function on the same file?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM