如何将一个反应成分传递到另一个反应成分中以超越第一个成分的含量?

JavaScript

神无L

2020-03-10

有没有办法将一个组件传递到另一个React组件?我想创建一个模型react组件,并传入另一个react组件以包含该内容。

编辑:这是一个reactJS codepen,说明了我正在尝试做的事情。http://codepen.io/aallbrig/pen/bEhjo

的HTML

<div id="my-component">
    <p>Hi!</p>
</div>

ReactJS

/**@jsx React.DOM*/

var BasicTransclusion = React.createClass({
  render: function() {
    // Below 'Added title' should be the child content of <p>Hi!</p>
    return (
      <div>
        <p> Added title </p>
        {this.props.children}
      </div>
    )
  }
});

React.renderComponent(BasicTransclusion(), document.getElementById('my-component'));

第522篇《如何将一个反应成分传递到另一个反应成分中以超越第一个成分的含量?》来自Winter(https://github.com/aiyld/aiyld.github.io)的站点

7个回答
GO前端 2020.03.10

Actually, your question is how to write a Higher Order Component (HOC). The main goal of using HOC is preventing copy-pasting. You can write your HOC as a purely functional component or as a class here is an example:

    class Child extends Component {
    render() {
        return (
            <div>
                Child
            </div>
        );
    }
}

If you want to write your parent component as a class-based component:

    class Parent extends Component {
    render() {
        return (
            <div>
                {this.props.children}
            </div>
        );
    }
}

If you want to write your parent as a functional component:

    const Parent=props=>{
    return(
        <div>
            {props.children}
        </div>
    )
}
JinJin小胖 2020.03.10

Facebook建议使用无状态组件来源:https : //facebook.github.io/react/docs/reusable-components.html

在理想的情况下,您的大多数组件都是无状态功能,因为在将来,我们还可以通过避免不必要的检查和内存分配来针对这些组件进行性能优化。如果可能,这是推荐的模式。

function Label(props){
    return <span>{props.label}</span>;
}

function Hello(props){
    return <div>{props.label}{props.name}</div>;
}

var hello = Hello({name:"Joe", label:Label({label:"I am "})});

ReactDOM.render(hello,mountNode);
逆天猿理查德 2020.03.10
const ParentComponent = (props) => {
  return(
    {props.childComponent}
    //...additional JSX...
  )
}

//import component
import MyComponent from //...where ever

//place in var
const myComponent = <MyComponent />

//pass as prop
<ParentComponent childComponent={myComponent} />
逆天西门 2020.03.10

我更喜欢使用React内置的API:

import React, {cloneElement, Component} from "react";
import PropTypes from "prop-types";

export class Test extends Component {
  render() {
    const {children, wrapper} = this.props;
    return (
      cloneElement(wrapper, {
        ...wrapper.props,
        children
      })
    );
  }
}

Test.propTypes = {
  wrapper: PropTypes.element,
  // ... other props
};

Test.defaultProps = {
  wrapper: <div/>,
  // ... other props
};

那么您可以将包装器div替换为所需的内容:

<Test wrapper={<span className="LOL"/>}>
  <div>child1</div>
  <div>child2</div>
</Test> 
小小Sam小哥 2020.03.10

You can pass in a component via. the props and render it with interpolation.

var DivWrapper = React.createClass({
    render: function() {
        return <div>{ this.props.child }</div>;
    }
});

You would then pass in a prop called child, which would be a React component.

Davaid前端神奇 2020.03.10

您可以将其作为常规道具传递: foo={<ComponentOne />}

例如:

const ComponentOne = () => <div>Hello world!</div>
const ComponentTwo = () => (
  <div>
    <div>Hola el mundo!</div>
    <ComponentThree foo={<ComponentOne />} />
  </div>
)
const ComponentThree = ({ foo }) => <div>{foo}</div>
MandyL 2020.03.10

您可以this.props.children用来渲染组件包含的任何子代:

const Wrap = ({ children }) => <div>{children}</div>

export default () => <Wrap><h1>Hello word</h1></Wrap>

问题类别

JavaScript Ckeditor Python Webpack TypeScript Vue.js React.js ExpressJS KoaJS CSS Node.js HTML Django 单元测试 PHP Asp.net jQuery Bootstrap IOS Android