美文网首页
react组件的使用

react组件的使用

作者: pawn_c | 来源:发表于2019-08-27 22:08 被阅读0次

紧接上一篇文章jsx的使用

在src下创建components文件夹
创建并编辑Hello.jsx文件

import React from 'react'

function Hello(props){
    return <a>姓名:{props.name}---年龄:{props.age}</a>
}
export default Hello

在index.js下导入

import Hello from './components/Hello.jsx'

index.js最终文件如下


//这两个导入的时候,接收的成员名称,必须这么写
import React from 'react'//创建组件、虚拟dom元素,生命周期
import ReactDOM from 'react-dom'//把创建好的组件和虚拟dom放到页面上展示

import Hello from './components/Hello.jsx'

/*
<div><h1 id = "myh1" title="this is title">hello pawn</h1></div>
*/

/*
html是最优秀的标记语言:
在js文件中,默认不能写这种类似于html的标记,否则打包会失败
可以使用babel来转换这些标签
这种在js中混合html的语法,叫做jsx语法,符合xml规范的js
但是jsx的本质还是转换成React.createElement来执行的
*/


const mydiv = <h1>hello ß1pawn</h1>

/*
参数1:要渲染的那个虚拟dom元素
参数2:指定页面上一个容器
*/
const people = {
    name:"pawn",
    age:24
}
ReactDOM.render(<div><Hello {...people}></Hello></div>,document.getElementById('app'))

然后在webpack.config.js里添加识别jsx的配置

module: {
        rules: [
            {
                test: /\.jsx$/,
                exclude: /node_modules/,
                use: {
                    loader: "babel-loader"
                }
            }
        ]
    }

最终如下

const path = require('path');
const HtmlWebPackPlugin = require('html-webpack-plugin');//导入 在内存中自动生成index页面的插件

//创建一个插件的实例对象
const htmlPlugin = new HtmlWebPackPlugin({
   template:path.join(__dirname,'./src/index.html'),//源文件
   filename:'index.html'//生成的内存中的首页名称
})
module.exports = {
   mode:'development', //development开发。production 发布
   plugins:[
       htmlPlugin
   ],
   module: {
       rules: [
           {
               test: /\.js|jsx$/,
               exclude: /node_modules/,
               use: {
                   loader: "babel-loader"
               }
           }
       ]
   }

}

重新运行,成功

相关文章

  • 面试题

    React组件的渲染流程是什么? 使用 React.createElement或 JSX编写 React组件,实际...

  • React: 富文本

    项目使用的是React,所以用的react-quill组件 1、安装 2、导入组件 3、导入样式 4、使用组件 页...

  • 高阶组件

    React 高阶组件HOC (Higher-Order Component) 高阶组件是react中重复使用组件逻...

  • react-draft-wysiwyg富文本组件

    目录 react-draft-wysiwyg富文本组件 react-draft-wysiwyg富文本组件 组件使用...

  • React学习笔记_02

    React 组件和状态 react 组件 1,组件的两种创建方式1,函数组件2,类组件 1,函数组件:使用 JS ...

  • 工作笔记三

    react-native 使用图标字体: 使用react-native-elements组件(需要先安装react...

  • React Native - 01 - Hello World!

    React Native就像React,但它使用本地组件而不是Web组件作为构建块。因此,要理解React Nat...

  • 记录react相关一些功能使用(持续更新)

    React-redux connect功能:连接容器组件和展示组件使用 React Redux 库的 connec...

  • ListView(1)酒列表

    React Native组件ListView的简单使用,但是React Native已经不推荐使用ListView了。

  • 第9节 React中的父子组件及传值2019-05-26

    一、React组件介绍 1.1React中的组件 解决html 标签构建应用的不足。 1.2使用组件的好处 把公共...

网友评论

      本文标题:react组件的使用

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