美文网首页React
React Web Component

React Web Component

作者: 天玑云象 | 来源:发表于2018-04-28 18:09 被阅读24次

React Web Component

Create component: Summary.js

Method 1, extend Component

import React from 'react'

class Summary extends React.Component {
  render() {
    const {ingredients, steps, title} = this.props
    return (
      <div className="summary">
        <h1>{title}</h1>
        <p>
          <span>{ingredients.length} Ingredients | </span>
          <span>{steps.length} Steps </span>
        </p>
      </div>
    )
  }
}

Method 2, stateless functional component

const Summary = ({ingredients, steps, title}) => {
  return <div>
    <h1>{title}</h1>
   <p>
     <span>{ingredients.length} Ingredients | </span>
     <span>{steps.length} Steps </span>
    </p>
  </div>
} 

or even more simpler:

const Summary = ({ingredients, steps, title}) =>
  <div>
    <h1>{title}</h1>
   <p>
     <span>{ingredients.length} Ingredients | </span>
     <span>{steps.length} Steps </span>
    </p>
  </div>

Use the component in main js: index.js

import React from 'react';
import ReactDOM from 'react-dom';
import Summary from './Summary';

ReactDOM.render(
  <Summary 
      title="Peanut Butter and Jelly"
      ingredients="peanut butter, jelly, bread"
      steps="spread peanut butter and jelly between bread" />, 
  document.getElementById('root')
);

相关文章

网友评论

    本文标题:React Web Component

    本文链接:https://www.haomeiwen.com/subject/ilfnlftx.html