React Set Image Props

Alex picture Alex · Jun 2, 2017 · Viewed 14.1k times · Source

Hello guys im new to REACT im trying to pass my image props to {logo} has anybody a clue how i can set it up ? 123 is displayed but i still miss the image

    export default class StickyHeader extends React.Component {

  static propTypes = {

  }

  static defaultProps = {

  }

  render() {

    const { logo } = this.props;

    return (
      <header>
        <div className={'logo'}>
          {logo}
        </div>
        <div>123</div>
      </header>
    );
  }
}

<StickyHeader logo={ <img src="http://via.placeholder.com/350x150" alt="" /> }></StickyHeader>

Answer

CD-jS picture CD-jS · Jun 2, 2017

In this case, I would put the image tag inside your sticky header component, and then pass just the logo url as a property, so your render method would look more like this:

render() {
    const { logoUrl } = this.props;

    return (
      <header>
        <div className={'logo'}>
          <img src={logoUrl} />
        </div>
        <div>123</div>
      </header>
    );
  }
}

and then your usage -

<StickyHeader logoUrl={'http://via.placeholder.com/350x150'} />