一、函数式组件
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>helloReact</title>
</head>
<body>
<div id="test"></div>
<script src="../js/react.development.js"></script>
<script src="../js/react-dom.development.js"></script>
<script src="../js/babel.min.js"></script>
<script type="text/babel">
function MyComponent () {
return <h2>我是函数定义的组件(适用于【简单组件】的定义)</h2>
}
ReactDOM.render(<MyComponent/>, document.getElementById('test'))
/*
1、react解析组件标签,找到MyComponent组件
2、发现组件是使用函数定义的,随后调用该函数,将返回的虚拟DOM转换为真实DOM,随后呈现在页面中
*/
</script>
</body>
</html>
二、类式组件
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>helloReact</title>
</head>
<body>
<div id="test"></div>
<script src="../js/react.development.js"></script>
<script src="../js/react-dom.development.js"></script>
<script src="../js/babel.min.js"></script>
<script type="text/babel">
class MyComponent extends React.Component{
render () {
return <h2>我是类定义的组件(适用于【复杂组件】的定义)</h2>
}
}
ReactDOM.render(<MyComponent/>, document.getElementById('test'))
/*
1、react解析组件标签,找到MyComponent组件
2、发现组件是使用类定义的,随后new出来该类的实例,并通过实例调用该原型上的render方法
3、将render返回的虚拟dom转换为真实dom,随后呈现在页面中
*/
</script>
</body>
</html>
函数式组件和类式组件
最新推荐文章于 2024-08-03 09:04:08 发布