" />

最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

你知道怎么在 HTML 頁(yè)面中使用 React嗎

 更新時(shí)間:2022年01月25日 16:43:27   作者:秋荷雨翔  
這篇文章主要為大家詳細(xì)介紹了如何在HTML頁(yè)面中使用 React,使用使用js模塊化的方式開(kāi)發(fā),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

該方案使用場(chǎng)景:在html頁(yè)面中使用react,主js文件index.js和其它非react功能使用js模塊化的方式開(kāi)發(fā),適合輕量級(jí)中小型應(yīng)用

index.html代碼:

引入react、react-dombabel、moment、antd

<!DOCTYPE html>
<html lang='zh-CN'>
<head>
    <title>React in HTML</title>
    <meta charset="utf-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="libs/antd/antd.min.css">
    <link rel="stylesheet" href="css/index.css">
    <style type="text/css">
    </style>
    <script type="text/javascript" src="libs/jquery-1.9.1.js"></script>
    <script type="text/javascript" src="libs/react/react.production.min.js"></script>
    <script type="text/javascript" src="libs/react/react-dom.production.min.js"></script>
    <script type="text/javascript" src="libs/babel/babel.min.js"></script>
    <script type="text/javascript" src="libs/moment/moment-with-locales.min.js"></script>
    <script type="text/javascript" src="libs/antd/antd-with-locales.min.js"></script>
</head>
<body>
    <input id='btn' type="button" class="index-btn" value="顯示React組件" />
    <script type="text/babel" src="components/HelloReact.jsx"></script>
    <script type="module" src="index.js"></script>
</body>
</html>

index.js代碼:

import { ReactComponentContainer } from './ReactComponentContainer.js'
let isShow = true;
let helloReactContainer;
$('#btn').on('click', function () {
    if (isShow) {
        helloReactContainer = new ReactComponentContainer('helloReact', HelloReact, { name: 'React' });
        helloReactContainer.show();
        isShow = false;
        $(this).val('隱藏React組件');
    } else {
        helloReactContainer.hide();
        isShow = true;
        $(this).val('顯示React組件');
    }
});

ReactComponentContainer.js代碼:

該模塊用于在html中顯示隱藏react組件

class ReactComponentContainer {
    component
    componentProps
    componentContainerId
    constructor(componentContainerId, component, componentProps) {
        if ($('#' + componentContainerId).length == 0) {
            $('body').append('<div id="' + componentContainerId + '"></div>');
        }
        this.componentContainerId = componentContainerId;
        this.component = component;
        this.componentProps = componentProps;
    }
    render(isShow) {
        ReactDOM.render(
            React.createElement(
                antd.ConfigProvider,
                {
                    locale: antd.locales.zh_CN
                },
                React.createElement(this.component, Object.assign({ isShow: isShow }, this.componentProps))
            ),
            document.getElementById(this.componentContainerId)
        );
    }
    show() {
        this.render(true);
    }
    hide() {
        this.render(false);
    }
}
export { ReactComponentContainer }

HelloReact.jsx代碼:

class HelloReact extends React.Component {
    dateFormat = 'YYYY-MM-DD'
    timeFormat = 'HH:mm:ss'
    constructor(props) {
        super(props);
        let now = new Date().valueOf();
        this.state = {
            dateStr: moment(now).format(this.dateFormat),
            timeStr: moment(now).format(this.timeFormat)
        }
        this.onChangeDate = this.onChangeDate.bind(this);
        this.onChangeTime = this.onChangeTime.bind(this);
        this.updateDatePickerAndTimePicker = this.updateDatePickerAndTimePicker.bind(this);
    }
    onChangeDate(date, dateString) {
        this.setState({ dateStr: dateString });
    }
    onChangeTime(time, timeString) {
        this.setState({ timeStr: timeString });
    }
    updateDatePickerAndTimePicker() {
        let now = new Date().valueOf();
        this.setState({
            dateStr: moment(now).format(this.dateFormat),
            timeStr: moment(now).format(this.timeFormat)
        });
    }
    render() {
        return <div style={{ display: this.props.isShow ? '' : 'none' }}>
            <h1>Hello {this.props.name}, Now is {this.state.dateStr} {this.state.timeStr}</h1>
            <antd.DatePicker onChange={this.onChangeDate} value={moment(this.state.dateStr, this.dateFormat)} />
            ?
            <antd.TimePicker onChange={this.onChangeTime} value={moment(this.state.timeStr, this.timeFormat)} />
            <br />
            <antd.Button type="primary" size="default" style={{ marginTop: '10px' }} onClick={this.updateDatePickerAndTimePicker} >更新日期時(shí)間控件值</antd.Button>
        </div>;
    }
}

效果圖:

瀏覽器按F12彈出DevTools,在Sources選項(xiàng)卡中可以看到組件代碼,方便打斷點(diǎn)調(diào)試

遇到的問(wèn)題:

無(wú)法使用es6的import語(yǔ)法導(dǎo)入react組件,es6的import和require.js都不認(rèn)識(shí)jsx

react組件不是按需加載,只適合小型應(yīng)用

Gitee代碼地址:

https://gitee.com/zjvngvn/react-in-html

總結(jié)

本篇文章就到這里了,希望能夠給你帶來(lái)幫助,也希望您能夠多多關(guān)注腳本之家的更多內(nèi)容!

相關(guān)文章

  • React創(chuàng)建組件的三種方式及其區(qū)別是什么

    React創(chuàng)建組件的三種方式及其區(qū)別是什么

    在React中,創(chuàng)建組件的三種主要方式是函數(shù)式組件、類組件和使用React Hooks的函數(shù)式組件,本文就詳細(xì)的介紹一下如何使用,感興趣的可以了解一下
    2023-08-08
  • React Fiber結(jié)構(gòu)的創(chuàng)建步驟

    React Fiber結(jié)構(gòu)的創(chuàng)建步驟

    這篇文章主要介紹了React Fiber結(jié)構(gòu)的創(chuàng)建步驟,幫助大家更好的理解和學(xué)習(xí)使用React,感興趣的朋友可以了解下
    2021-04-04
  • react如何修改循環(huán)數(shù)組對(duì)象的數(shù)據(jù)

    react如何修改循環(huán)數(shù)組對(duì)象的數(shù)據(jù)

    這篇文章主要介紹了react如何修改循環(huán)數(shù)組對(duì)象的數(shù)據(jù)問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-12-12
  • react-native 封裝視頻播放器react-native-video的使用

    react-native 封裝視頻播放器react-native-video的使用

    本文主要介紹了react-native 封裝視頻播放器react-native-video的使用,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2023-01-01
  • react配置px轉(zhuǎn)換rem的方法

    react配置px轉(zhuǎn)換rem的方法

    這篇文章主要介紹了react配置px轉(zhuǎn)換rem的方法,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2021-04-04
  • 基于React Context實(shí)現(xiàn)一個(gè)簡(jiǎn)單的狀態(tài)管理的示例代碼

    基于React Context實(shí)現(xiàn)一個(gè)簡(jiǎn)單的狀態(tài)管理的示例代碼

    本文主要介紹了基于React Context實(shí)現(xiàn)一個(gè)簡(jiǎn)單的狀態(tài)管理的示例代碼,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2022-07-07
  • React通過(guò)conetxt實(shí)現(xiàn)多組件傳值功能

    React通過(guò)conetxt實(shí)現(xiàn)多組件傳值功能

    Context 提供了一種在組件之間共享此類值的方式,而不必顯式地通過(guò)組件樹(shù)的逐層傳遞 props。本文給大家介紹React通過(guò)conetxt實(shí)現(xiàn)多組件傳值功能,感興趣的朋友一起看看吧
    2021-10-10
  • React-three-fiber使用初體驗(yàn)

    React-three-fiber使用初體驗(yàn)

    這篇文章主要為大家介紹了React-three-fiber的使用初體驗(yàn),有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-05-05
  • React/Redux應(yīng)用使用Async/Await的方法

    React/Redux應(yīng)用使用Async/Await的方法

    本篇文章主要介紹了React/Redux應(yīng)用使用Async/Await的方法,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2017-11-11
  • React18新增特性released的使用

    React18新增特性released的使用

    本文主要介紹了React18新增特性released的使用,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2022-05-05

最新評(píng)論

台南县| 万源市| 长治县| 禄丰县| 潜江市| 枞阳县| 宁阳县| 阿鲁科尔沁旗| 涪陵区| 南华县| 宜宾市| 克东县| 化隆| 肇源县| 绥德县| 晋城| 内乡县| 桐梓县| 麟游县| 鸡东县| 梧州市| 广元市| 个旧市| 龙江县| 通化市| 石柱| 双辽市| 磴口县| 新营市| 海原县| 台东市| 大英县| 岱山县| 哈尔滨市| 威远县| 淅川县| 长泰县| 石台县| 分宜县| 八宿县| 霍林郭勒市|