简体   繁体   English

如何将扩展React.Component的类作为属性传递给它(使用babel)?

[英]How to pass a class extending React.Component as a property to render it (using babel)?

This issue is only reproducable in babel (using babel-runtime-6.26.0). 此问题仅在babel中可重现(使用babel-runtime-6.26.0)。

Having the following structure 具有以下结构

class Foo extends React.Component {
// ...
}

class Bar extends React.Component {
  render() {
    return this.props.component();
  }
}

// somewhere:
<Bar component={Foo} />

worked and works for me in general, but when switching to babel I get the error message: TypeError: Cannot call a class as a function in (classCallCheck.js:7) . 总的来说对我来说是(classCallCheck.js:7) ,但是在切换到babel时,我收到错误消息: TypeError: Cannot call a class as a function(classCallCheck.js:7) TypeError: Cannot call a class as a function I've read that this is spec compliant behavior, but it does work outside of the babel environment. 我已经读到这是符合规范的行为,但是它确实在babel环境之外起作用。

Btw, if I use functional stateless components it works, of course. 顺便说一句,如果我使用功能性无状态组件,那当然可以。 But I can't guarantee that the consumer of my lib is providing such a component and not a class based one. 但是我不能保证我的库的使用者提供了这样的组件,而不是基于类的组件。

  • How can I approach this using babel? 如何使用Babel来解决这个问题?
  • Is there a different pattern to use to pass components via properties? 是否有其他可用于通过属性传递组件的模式? I believe it's common practice to pass in classes/class constructors via props to components. 我相信,通过props传递给组件的类/类构造函数是常见的做法。

A component can be either a function or a class. 组件可以是函数或类。 And ES6 classes cannot be called without new , at least the ones that are spec-compliant (this includes native and Babel classes). 而且,如果没有new ,则不能调用ES6类,至少要符合规范(包括本地类和Babel类)。

This is not a problem since components shouldn't be instantiated manually. 这不是问题,因为不应手动实例化组件。 Both function and class components will be properly handled by ReactDOM.render : 函数和类组件都将由ReactDOM.render正确处理:

class Bar extends React.Component {
  render() {
    const Component = this.props.component;
    return <Component/>;
  }
}

Try using React.createElement(this.props.component, props) instead. 尝试React.createElement(this.props.component, props)使用React.createElement(this.props.component, props)

Here's a working example: 这是一个工作示例:

class Foo extends React.Component {
  render() { return "Foo" }
}

class Bar extends React.Component {
  render() {
    return React.createElement(this.props.component, {})
  }
}

ReactDOM.render(
  <Bar component={Foo} />,
  document.getElementById('root')
);

https://codepen.io/anon/pen/QQGQYZ https://codepen.io/anon/pen/QQGQYZ

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将数据从 class React.Component 传递到 jsx 文件 - How to pass data from class React.Component to jsx file 我们如何在 react-native 功能组件中声明和使用 class 而不用 React.Component 扩展它? - How can we declare and use class in react-native functional component without extending it with React.Component? 如何扩展包装好的 React.Component class? - How to extend a wrapped React.Component class? 如何从 CDN 加载 React.Component 并渲染到另一个 React.Component - How to load a React.Component from a CDN and render into another React.Component REDUX。我无法理解如何连接定义为扩展 React.Component 的 class 的组件以读取商店 - REDUX. I cant understand how to connect a component defined as a class extending React.Component in order to read the store class 可以是 object 的属性吗? 这就是 React.Component 吗? - Can a class be a property of an object? Is this what React.Component is? 如何在 class 扩展 React.Component 中将 this.variable 传递给 HTML? - How to pass this.varible to HTML within class extends React.Component? 如何在鼠标进入/离开事件中呈现React.Component - How to render React.Component on mouse enter/leave events 如何在defaultProps React.Component类语法中使用&#39;this&#39;? - How can I use 'this' in defaultProps React.Component class syntax? 为什么扩展React.Component内的方法语法不同? - Why are there different syntax of methods inside extending React.Component?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM