React系列(一)入门篇

中文官方网站:https://react.docschina.org

一、简单的Hello World例子

例子中引入:三个JS

  • babel.min.js:让浏览器兼容ES6,将jsx转换成js
  • react.development.js:React的核心库
  • react-dom.development.js:React扩展库
<!Doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Hello World</title>
</head>
<body>
    <!-- 准备好一个容器 -->
    <div id="test"></div>

    <!--引入React 核心库-->
    <script type="text/javascript" src="../../js/react.development.js"></script>
    <!--引入React-dom 用于支持React操作DOM-->
    <script type="text/javascript" src="../../js/react-dom.development.js"></script>
    <!--引入babel 用于Jsx转换称js-->
    <script type="text/javascript" src="../../js/babel.min.js"></script>

    <script type="text/babel">
        //1、创建一个虚拟的DOM
        const VDOM = <h1>Hello world</h1>
        //2、渲染虚拟DOM到页面
        ReactDOM.render(VDOM,document.getElementById('test'))
    </script>
</body>
</html>
PHP&前端、基础技术React系列(一)入门篇插图

二、jsx和js的例子对比

2.1jsx的例子

<!Doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Hello World</title>
</head>
<body>
    <!-- 准备好一个容器 -->
    <div id="test"></div>

    <!--引入React 核心库-->
    <script type="text/javascript" src="../../js/react.development.js"></script>
    <!--引入React-dom 用于支持React操作DOM-->
    <script type="text/javascript" src="../../js/react-dom.development.js"></script>
    <!--引入babel 用于Jsx转换称js-->
    <script type="text/javascript" src="../../js/babel.min.js"></script>

    <script type="text/babel">
        //1、创建一个虚拟的DOM
        const VDOM = (
            <h1>Hello world
                <span> by jsx demo</span>
            </h1>
        )
        //2、渲染虚拟DOM到页面
        ReactDOM.render(VDOM,document.getElementById('test'))
    </script>
</body>
</html>

2.2js的例子

<!Doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Hello World</title>
</head>
<body>
    <!-- 准备好一个容器 -->
    <div id="test"></div>

    <!--引入React 核心库-->
    <script type="text/javascript" src="../../js/react.development.js"></script>
    <!--引入React-dom 用于支持React操作DOM-->
    <script type="text/javascript" src="../../js/react-dom.development.js"></script>
    <!--引入babel 用于Jsx转换称js-->
    <script type="text/javascript" src="../../js/babel.min.js"></script>

    <script type="text/babel">

        //1、创建一个虚拟的DOM
        // const VDOMChild1 = React.createElement('li', null, 'one');
        // const VDOMChild2 = React.createElement('li', null, 'two');
        // const ParentVDOM = React.createElement('ul', { className: 'teststyle' }, [child1, child2]);
        const VDOM = React.createElement(
            "h1",
            {id:"h1-id",class:"h1-class"},
            React.createElement(
                "span",
                {},
                "Hello world by js demo")
            )
        //2、渲染虚拟DOM到页面
        ReactDOM.render(VDOM,document.getElementById('test'))
    </script>
</body>
</html>

三、jsx的语法规则

  1. 定义DOM时不能有单引号
  2. 标签中混入JS表达式时用{}
  3. 当声明标签的类的需要用className
  4. 如果需要内联style的时候,用双花括号style={{key:value}}
  5. 虚拟DOM只能有一个根标签
  6. 标签必须闭合,例如<input>需要写成<input type=”text” />,比如 <div>...</div> 或使用空的 <>...</>
  7. 标签首字母是小写则会转成同名元素
  8. 标签首字母是大写则则会渲染对应的组件,没有就会报错

什么是Js表达式?什么是js语句

以下是JS表达式:一个表达式会产生一个值

1、a
2、a+b/a-b
3、demo(1) //调用函数
4、arr.map()
5、function test(){}

以下是JS语句

1. if
2.for
3.switch(){case:xxxx}

3.1 jsx例子

3.1.1简单显示数据

    <script type="text/babel">
        //1、创建一个虚拟的DOM
        const title = "This is a title";
        const VDOM = (
            <>
                <h1 id="h1-id1" >
                    <p>The title is :{title}</p>
                </h1>
               
            </>
        )
        //2、渲染虚拟DOM到页面
        ReactDOM.render(VDOM,document.getElementById('test'))
    </script>
PHP&前端、基础技术React系列(一)入门篇插图1

3.1.2复杂显示数据

    <script type="text/babel">
        //1、创建一个虚拟的DOM
        const postInfo = {
            title: 'the post title',
            content: 'the post content',
            author: 'fox',
            color: 'red'
        };
        const VDOM = (
            <>
                <h1 id="h1-id1" >
                    <a 
                        alt={'author of ' + postInfo.author} 
                        style={{
                            color: postInfo.color,
                            fontSize: '15px'
                        }}
                    >
                        The title is :{postInfo.title.toUpperCase()}
                    </a>
                </h1>
            </>
        )
        //2、渲染虚拟DOM到页面
        ReactDOM.render(VDOM,document.getElementById('test'))
PHP&前端、基础技术React系列(一)入门篇插图2

3.1.3渲染数组数据

<!Doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Hello World</title>
</head>
<body>
    <!-- 准备好一个容器 -->
    <div id="test"></div>

    <!--引入React 核心库-->
    <script type="text/javascript" src="../../js/react.development.js"></script>
    <!--引入React-dom 用于支持React操作DOM-->
    <script type="text/javascript" src="../../js/react-dom.development.js"></script>
    <!--引入babel 用于Jsx转换称js-->
    <script type="text/javascript" src="../../js/babel.min.js"></script>

    <script type="text/babel">
        //1、创建一个虚拟的DOM
        const data = ["张三","李四","王五"];
        const data2 = [
                        { title: 'Cabbage', id: 5 },
                        { title: 'Garlic', id: 6 }
                    ];
        const data3 = [
            { title: 'fox', id: 7 },
            { title: 'fcors', id: 8 }
        ]; 
        const listItem = data3.map(data3Item=>{
            return <li key={data3Item.id}>
                {data3Item.title}
            </li>
        })
        const VDOM = (
            <>
                <ul>
                   
                    {
                        data.map((item,index)=>{
                         return <li key={index}>{item}</li>
                        })
                        
                    }  
                    /*方法一*/
                    {
                        data2.map(product =>
                        <li key={product.id}>
                            {product.title}
                        </li>
                        )
                        
                    }
                    /*方法二*/
                    {listItem}       
                </ul>
            </>
        )
        //2、渲染虚拟DOM到页面
        ReactDOM.render(VDOM,document.getElementById('test'))
    </script>
</body>
</html>

3.1.4 响应事件

简单的click事件

    <script type="text/babel">
        function handleClick() {
            alert('You clicked me!');
        }
        //1、创建一个虚拟的DOM
        const VDOM = (
            <h1 id="h1-id" onClick={handleClick} className="h1-class">
                <span>Hello world by jsx demo</span>
            </h1>
        )
        //2、渲染虚拟DOM到页面
        ReactDOM.render(VDOM,document.getElementById('test'))
    </script>

传参式click事件

<!Doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Hello World</title>
</head>
<body>
    <!-- 准备好一个容器 -->
    <div id="test"></div>

    <!--引入React 核心库-->
    <script type="text/javascript" src="../../js/react.development.js"></script>
    <!--引入React-dom 用于支持React操作DOM-->
    <script type="text/javascript" src="../../js/react-dom.development.js"></script>
    <!--引入babel 用于Jsx转换称js-->
    <script type="text/javascript" src="../../js/babel.min.js"></script>

    <script type="text/babel">
      
        const handleClick = (event, name) => {
            event.preventDefault();
            const buttonId = event.target.id;
            const showStatus = event.target.getAttribute('data-showstatus');
            event.target.setAttribute('data-showstatus', 'show');
            event.target.classList.add('show');
            console.log(event.target);
            console.log(`Button clicked: ${buttonId}`);
            console.log(`Show status: ${showStatus}`);
            const classNames = event.target.classList;
            console.log(classNames);
        }

        const VDOM =  (
            <div>
            <button onClick={(event) => handleClick(event,"World")}>
                Click me
            </button>
            </div>
        );

        //2、渲染虚拟DOM到页面
        ReactDOM.render(VDOM,document.getElementById('test'))
    </script>
</body>
</html>
https://www.fcors.com/%e6%8a%80%e6%9c%af%e4%b8%8e%e6%a1%86%e6%9e%b6/%e5%9f%ba%e7%a1%80%e6%8a%80%e6%9c%af/react%e7%b3%bb%e5%88%97%ef%bc%88%e4%ba%8c%ef%bc%89%e7%bb%84%e4%bb%b6%e5%8f%8astate/