1 Star 0 Fork 0

Bo/react-from-zero

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
06-property-types.html 1.58 KB
一键复制 编辑 原始数据 按行查看 历史
<!doctype html>
<title>06 Property Types - React From Zero</title>
<script src="https://unpkg.com/react@16.4.0/umd/react.development.js"></script>
<script src="https://unpkg.com/react-dom@16.4.0/umd/react-dom.development.js"></script>
<script src="https://unpkg.com/@babel/standalone/babel.min.js"></script>
<script src="https://unpkg.com/prop-types@15.6.1/prop-types.js">
// PropTypes were removed from React 16 and are now their own package
</script>
<div id="app"></div>
<script type="text/babel">
// Components get created to encapsulate stuff that should be together in one
// place and for reuse.
// Reuse requires the user of the component to supply the correct properties so
// we can define a type of each property and set defaults
function MyComponent(props) {
return (
<div className={props.className}>
<h1>Hello</h1>
<h2>{props.customData}</h2>
</div>
)
}
// Add the propTypes (function-)property to the component function
// to let it validate its (element-)properties
MyComponent.propTypes = {
// React supplies us with a bunch of types, like string
customData: PropTypes.string,
}
// Add defaultProps (function-)property to set the defaults
// if nothing was provided by the user
MyComponent.defaultProps = {
customData: "default",
className: "default-class",
}
// This will show a warning in the console, because customData should be a string
var reactElement = <MyComponent customData={123}/>
// This will use the defaults
reactElement = <MyComponent/>
var renderTarget = document.getElementById("app")
ReactDOM.render(reactElement, renderTarget)
</script>
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/uyscuti/react-from-zero.git
git@gitee.com:uyscuti/react-from-zero.git
uyscuti
react-from-zero
react-from-zero
master

搜索帮助