简体   繁体   中英

Passing Programmatically props to a Component Object

Is that possible to add programmatically a prop to an existing React Component.

Example :

// 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} />
}

Can I do something like this ?

const myProps = { content: 'ABCDEF' }
const enhanced = compose(GenericContent, myProps)

I'm not sure I've understanding your question properly, What do you mean by this compose(GenericContent, myProps)

Is that what are you trying to accomplish?

编辑react-google-maps示例

Main App

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 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM