在HTML页面中如何使用React
发表于:2024-10-24 作者:千家信息网编辑
千家信息网最后更新 2024年10月24日,这篇文章主要介绍了在HTML页面中如何使用React的相关知识,内容详细易懂,操作简单快捷,具有一定借鉴价值,相信大家阅读完这篇在HTML页面中如何使用React文章都会有所收获,下面我们一起来看看吧
千家信息网最后更新 2024年10月24日在HTML页面中如何使用React
这篇文章主要介绍了在HTML页面中如何使用React的相关知识,内容详细易懂,操作简单快捷,具有一定借鉴价值,相信大家阅读完这篇在HTML页面中如何使用React文章都会有所收获,下面我们一起来看看吧。
该方案使用场景:在html页面中使用react,主js文件index.js和其它非react功能使用js模块化的方式开发,适合轻量级中小型应用
index.html代码:
引入react
、react-dom
、babel
、moment
、antd
等
React in 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(''); } 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; }}Hello {this.props.name}, Now is {this.state.dateStr} {this.state.timeStr}
更新日期时间控件值
效果图:
浏览器按F12弹出DevTools,在Sources选项卡中可以看到组件代码,方便打断点调试
遇到的问题:
无法使用es6的import语法导入react组件,es6的import和require.js都不认识jsx
react组件不是按需加载,只适合小型应用
关于"在HTML页面中如何使用React"这篇文章的内容就介绍到这里,感谢各位的阅读!相信大家对"在HTML页面中如何使用React"知识都有一定的了解,大家如果还想学习更多知识,欢迎关注行业资讯频道。
组件
页面
代码
知识
内容
模块
篇文章
应用
中小
中小型
价值
功能
场景
控件
操作简单
效果
效果图
文件
文章
方式
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
车辆管理数据库设计
上海网络软件开发服务平均价格
软件开发公司 www
linux下dns服务器
中国丝绸博物馆里的数据库
软件开发工作量如何评估
深圳专业软件开发哪里好
网络技术就业前景好吗
湛江棋牌软件开发
参与赌博软件开发怎么处罚
云鼎科技与网络安全
开源voip服务器
软件开发的需求分析报告
数据库字段去重复数据
Max脚本如何下载服务器文件
办公智能化管理打印服务器
java的数据库类型分为
广州紫鲸网络技术有限公司
方舟服务器名字在哪搜索
在教学中融入现代网络技术
怎么找论文中的数据库
邯郸c语言软件开发哪家正规
广州咨询群控软件开发商
python的数据库操作
广州java软件开发公司
花都物联网软件开发设计
树莓派做无线服务器
网络安全计算机行业基金
滨州市远卓软件开发有限公司
软件开发管理 情况