以编程方式传递道具到组件对象

时间:2018-04-15 23:17:53

标签: reactjs inheritance

是否可以通过编程方式将prop添加到现有的React Component。

示例:

if (componentModel.LifestyleType == LifestyleType.Undefined)
            componentModel.LifestyleType = LifestyleType.Transient;

我可以这样做吗?

// GenericComp
class GenericComp extends Component {
  render = () => <Fragment>{this.props.content ? this.props.content : null}</Fragment>
}

export { GenericComp }

// AnotherFile
import { GenericComp }

// Is that possible to pass content without creating a new class like the // below :
class NewComp extends Component {
  render = () => <GenericComp content={myContent} />
}

1 个答案:

答案 0 :(得分:0)

我不确定我是否正确理解了您的问题,这是什么意思compose(GenericContent, myProps)

你想要完成的是什么?

Edit react-google-maps example

主要应用

import React, { Component } from 'react';
import GenericComp from './GenericComp';


const myProps = { text: 'ABCDEF' }

class Main extends Component {
  render = () => <GenericComp content={myProps} />
}

export default Main 

<强> GenericCop.js

import React, { Component } from 'react';

class GenericComp extends Component {
  render = () => <div> {this.props.content ? this.props.content.text : null}</div>
}

export default GenericComp