这是一些非常整洁的重构。你应该试着去做,而不是让它工作。这是因为当你把一个组件作为道具传递时,它是没有状态的。它需要在内部实例化。
第js节
import React from 'react'
import PropTypes from 'prop-types'
const Section = ({ children, icon, title, background, fullWidth }) => (
<section style={{background:background}}>
<div class="wrap">
<h2>{title}</h2>
<span class="icon">{icon()}</span> {/* Notice this line */}
<div class="content">
{children}
</div>
</div>
</section>
)
Section.propTypes = {
title: PropTypes.string,
icon: PropTypes.func, // Notice this line
background: PropTypes.string,
fullWidth: PropTypes.boolean,
}
Section.defaultProps = {
title: ``,
icon: ``,
background: ``,
fullWidth: false,
}
export default Section