React基础语法学习

article/2025/6/28 3:12:21

React主要有如下3个特点:

  • 作为UI(Just the UI)
  • 虚拟DOM(Virtual DOM):这是亮点 是React最重要的一个特性 放进内存 最小更新的视图,差异部分更新 diff算法
  • 数据流(Date Flow)单向数据流

学习React需要掌握哪些知识?

  • JSX语法 类似XML
  • ES6相关知识
  • 前端基础 CSS+DIV JS

例子一
(简单组件和数据传递) 使用this.props 指向自己的属性

<!DOCTYPE html>
<html>
<head lang="en"><meta charset="UTF-8"><title>React第一个例子</title><script type="text/javascript" src="react.js"></script><script type="text/javascript" src="react-dom.js"></script><script type="text/javascript" src="browser.min.js"></script>
</head>
<body>
<div id="example"></div>
<script type="text/babel">var HelloMessage=React.createClass({render:function(){return <h1 style={{color:'#ff0000',fontSize:'24px'}} >Hello {this.props.name}!我是东方耀</h1>;//这是注释  React.createElement}});ReactDOM.render(<HelloMessage name="React 语法基础8" /> ,document.getElementById('example'));</script>
</body>
</html>

例子二(通过this.state更新视图)

<!DOCTYPE html>
<html>
<head lang="en"><meta charset="UTF-8"><title>React第二个例子</title><script type="text/javascript" src="react.js"></script><script type="text/javascript" src="react-dom.js"></script><script type="text/javascript" src="browser.min.js"></script>
</head>
<body><div id="example"></div><script type="text/babel">var Timer=React.createClass({/*初始状态*/getInitialState:function(){return {secondsElapsed:0};},tick:function(){this.setState({secondsElapsed:this.state.secondsElapsed+1});},/*组件完成装载*/componentDidMount:function(){this.interval=setInterval(this.tick,1000);},/*组件将被卸载   清除定时器*/componentWillUnmount:function(){clearInterval(this.interval);},/*渲染视图*/render:function(){return(<div> Seconds Elapsed:{this.state.secondsElapsed} </div>);}});React.render( <Timer /> ,document.getElementById('example'));</script></body>
</html>

例子三(简单应用)

<!DOCTYPE html>
<html>
<head lang="en"><meta charset="UTF-8"><title>React第三个例子</title><script type="text/javascript" src="react.js"></script><script type="text/javascript" src="react-dom.js"></script><script type="text/javascript" src="browser.min.js"></script>
</head>
<body><div id="example"></div><script type="text/babel">var ShowEditor=React.createClass({getInitialState:function(){return {value:'你可以在这里输入文字'};},handleChange:function(){this.setState({value:React.findDOMNode(this.refs.textarea).value});},render:function(){return (<div><h3>输入</h3><textarea style={{width:300,height:150,outline:'none'}}onChange={this.handleChange}ref="textarea"defaultValue={this.state.value}/><h3>输出</h3><div> {this.state.value} </div></div>);}});React.render(<ShowEditor />,document.getElementById('example'));</script></body>
</html>

flexbox布局

伸缩项目的属性

1.order
定义项目的排列顺序,数值越小,排列越靠前,默认值为0,语法为:order:整数值
2.flex-grow
定义伸缩项目的放大比例,默认值为0,即表示如果存在剩余空间,也不放大,语法为:flex-grow:整
数值
3.flex-shrink
定义伸缩项目的收缩能力,默认值为1 ,其语法为:flex-shrink:整数值
4.flex-basis
用来设置伸缩项目的基准值,剩余的空间按比率进行伸缩,其语法为:flex-basis:length | auto,默认
值为auto
5.flex
是flex-grow flex-shrink flex-basis这三个属性的缩写,其语法为:flex:none | flex-grow flex-shrink flexbasis,
其中第二个和第三个参数为可选参数,默认值为:0 1 auto
6.align-self
用来设置单独的伸缩项目在交叉轴上的对齐方式,会覆盖默认的对齐方式,其语法为:align-self:auto
| flex-start | flex-end | center | baseline | stretch(伸缩项目在交叉轴方向占满伸缩容器,如果交叉轴为
垂直方向的话,只有在不设置高度的情况下才能看到效果)

在React Native中使用flexbox

RN目前主要支持flexbox的如下6个属性:
1.alignItems
用来定义伸缩项目在交叉轴上的对齐方式,语法为: alignItems:flex-start(默认值) | flex-end |
center | stretch
2.alignSelf
用来设置单独的伸缩项目在交叉轴上的对齐方式,会覆盖默认的对齐方式,其语法为:alignSelf:auto |
flex-start | flex-end | center | stretch(伸缩项目在交叉轴方向占满伸缩容器,如果交叉轴为垂直方向的
话,只有在不设置高度的情况下才能看到效果)
3.flex
是flex-grow flex-shrink flex-basis这三个属性的缩写,其语法为:flex:none | flex-grow flex-shrink flexbasis,
其中第二个和第三个参数为可选参数,默认值为:0 1 auto
4.flexDirection
指定主轴的方向 flex-direction:row| row-reverse | column(默认值) | column-reverse
5.flexWrap
6.justifyContent

BOX实现

<!DOCTYPE html>
<html><head><meta charset="utf-8"><title></title><style>.height50 {height: 50px;}.height400 {height: 400px;}.height300 {height: 300px;}.height200 {height: 200px;}.height100 {height: 100px;}.width400 {width: 400px;}.bgred {background-color: #6AC5AC;}.bggreen {background-color:  #414142;}.bgyellow {background-color: #D64078;}.box {display: flex;flex-direction: column;flex: 1;position: relative;color: #FDC72F;line-height: 1em;}.label {top: 0;left: 0;padding: 0 3px 3px 0;position: absolute;background-color: #FDC72F;color: white;line-height: 1em;}.top {width: 100%;justify-content: center;display: flex;align-items: center;}.bottom {width: 100%;display: flex;justify-content: center;align-items: center;}.right {width: 50px;display: flex;justify-content: space-around;align-items: center;}.left {width: 50px;display: flex;justify-content: space-around;align-items: center;}.heightdashed {position: absolute;right: 20px;height: 100%;border-left: 1px solid #FDC72F;}.widthdashed {position: absolute;left: 0px;width: 100%;bottom: 24px;border-top: 1px solid #FDC72F;}.margginBox {position:absolute;top: 100px;padding-left:7px;padding-right:7px;}.borderBox {flex: 1;display: flex;justify-content: space-between;}.paddingBox {flex: 1;display: flex;justify-content: space-between;}.elementBox{flex: 1;display: flex;justify-content: space-between;}.measureBox {flex: 1;display: flex;justify-content: flex-end;align-items: flex-end;}</style></head><body><span class="margginBox"><span class="box height400  width400"><span class="label">margin</span><span class="top height50 bgred">top</span><span class="borderBox"><span class="left bgred">left</span><span class="box height300  "><span class="label">border</span><span class="top height50 bggreen">top</span><span class="paddingBox"><span class="left bggreen">left</span><span class="box height200  "><span class="label">padding</span><span class="top height50 bgyellow">top</span><span class="elementBox"><span class="left bgyellow">left</span><span class="box height100  "><span class="label">element</span><span class="widthdashed"></span><span class="heightdashed"></span><span class="measureBox"><span class="right">height</span></span><span class="bottom  height50">width</span></span><span class="right bgyellow">right</span></span><span class="bottom  height50 bgyellow">bottom</span></span><span class="right bggreen">right</span></span><span class="bottom  height50 bggreen">bottom</span></span><span class="right bgred">right</span></span><span class="bottom  height50 bgred">bottom</span></span></span></body>
</html>

效果如下

这里写图片描述

ReactNative版实现

/*** Sample React Native App* https://github.com/facebook/react-native*/
'use strict';
import React, {AppRegistry,Component,StyleSheet,Text,View
} from 'react-native';class DongFang extends Component {render() {return (<View style={[BoxStyles.margginBox]}  ref="lab1"><View style={[BoxStyles.box,BoxStyles.height400,BoxStyles.width400]}><View style={[BoxStyles.top,BoxStyles.height50,BoxStyles.bgred]}><Text style={BoxStyles.yellow}>top</Text></View><View style={[BoxStyles.borderBox]}><View style={[BoxStyles.left,BoxStyles.bgred]} ><Text style={BoxStyles.yellow}>left</Text></View><View style={[BoxStyles.box,BoxStyles.height300]}><View style={[BoxStyles.top,BoxStyles.height50,BoxStyles.bggreen]}><Text style={BoxStyles.yellow}>top</Text></View><View style={[BoxStyles.paddingBox]}><View style={[BoxStyles.left,BoxStyles.bggreen]} ><Text style={BoxStyles.yellow}>left</Text></View><View style={[BoxStyles.box,BoxStyles.height200]}><View style={[BoxStyles.top,BoxStyles.height50,BoxStyles.bgyellow]}><Text style={BoxStyles.yellow}>top</Text></View><View style={[BoxStyles.elementBox]}><View style={[BoxStyles.left,BoxStyles.bgyellow]} ><Text style={BoxStyles.yellow}>left</Text></View><View style={[BoxStyles.box,BoxStyles.height100]}><View  style={[BoxStyles.label]}><Text style={BoxStyles.white}>element</Text></View><View style={[BoxStyles.widthdashed]} ></View><View style={[BoxStyles.heightdashed]} ></View><View style={[BoxStyles.measureBox]} ><View style={[BoxStyles.right]}><Text style={[BoxStyles.yellow]}>height</Text></View></View><View style={[BoxStyles.bottom,BoxStyles.height50]}><Text style={BoxStyles.yellow}>width</Text></View></View><View style={[BoxStyles.right,BoxStyles.bgyellow]}><Text style={BoxStyles.yellow}>right</Text></View></View><View style={[BoxStyles.bottom,BoxStyles.height50,BoxStyles.bgyellow]}><Text style={BoxStyles.yellow}>bottom</Text></View><View style={[BoxStyles.label]}><Text style={BoxStyles.white}>padding</Text></View></View><View style={[BoxStyles.right,BoxStyles.bggreen]}><Text style={BoxStyles.yellow}>right</Text></View></View><View style={[BoxStyles.bottom,BoxStyles.height50,BoxStyles.bggreen]}><Text style={BoxStyles.yellow}>bottom</Text></View><View style={[BoxStyles.label]}><Text style={BoxStyles.white}>border</Text></View></View><View style={[BoxStyles.right,BoxStyles.bgred]}><Text style={BoxStyles.yellow}>right</Text></View></View><View style={[BoxStyles.bottom,BoxStyles.height50,BoxStyles.bgred]}><Text style={BoxStyles.yellow}>bottom</Text></View><View style={[BoxStyles.label]} ><Text style={BoxStyles.white}>margin</Text></View></View></View>);}
}const BoxStyles = StyleSheet.create({height50:{height: 50,},height400:{height: 400,},height300:{height: 300,},height200:{height: 200,},height100:{height: 100,},width400:{width: 400,},width300:{width: 300,},width200:{width: 200,},width100:{width: 100,},bgred: {backgroundColor:'#6AC5AC',},bggreen: {backgroundColor: '#414142',},bgyellow: {backgroundColor: '#D64078',},box: {flexDirection: 'column',flex: 1,position: 'relative',},label: {top: 0,left: 0,paddingTop: 0,paddingRight: 3,paddingBottom: 3,paddingLeft: 0,position: 'absolute',backgroundColor: '#FDC72F',},top: {justifyContent: 'center',alignItems: 'center',},bottom: {justifyContent: 'center',alignItems: 'center',},right: {width: 50,justifyContent: 'space-around',alignItems: 'center',},left: {width: 50,justifyContent: 'space-around',alignItems: 'center',},heightdashed: {bottom: 0,top: 0,right: 20,borderLeftWidth: 1,position: 'absolute',borderLeftColor: '#FDC72F',},widthdashed: {bottom: 25,left: 0,right: 0,borderTopWidth: 1,position: 'absolute',borderTopColor: '#FDC72F',},yellow: {color: '#FDC72F',fontWeight:'900',},white: {color: 'white',fontWeight:'900',},margginBox:{position: 'absolute',top: 100,paddingLeft:7,paddingRight:7,},borderBox:{flex: 1,justifyContent: 'space-between',flexDirection: 'row',},paddingBox:{flex: 1,justifyContent: 'space-between',flexDirection: 'row',},elementBox:{flex: 1,justifyContent: 'space-between',flexDirection: 'row',},measureBox:{flex: 1,flexDirection: 'row',justifyContent: 'flex-end',alignItems:'flex-end',},container: {flex: 1,justifyContent: 'center',alignItems: 'center',backgroundColor: '#F5FCFF',},welcome: {fontSize: 20,textAlign: 'center',margin: 10,},instructions: {textAlign: 'center',color: '#333333',marginBottom: 5,},
});AppRegistry.registerComponent('DongFang', () => DongFang);

这里写图片描述


http://chatgpt.dhexx.cn/article/0j3k4nr6.shtml

相关文章

class语法糖,尝尝嘛~~

语法糖&#xff08;Syntactic sugar&#xff09;&#xff0c;也译为糖衣语法&#xff0c;是由英国计算机科学家彼得约翰兰达&#xff08;Peter J. Landin&#xff09;发明的一个术语&#xff0c;指计算机语言中添加的某种语法&#xff0c;这种语法对语言的功能并没有影响,但是更…

一种小型后台管理系统通用开发框架中的Cache缓存设计 ES6学习笔记 探秘 flex 上下文中神奇的自动 margin...

一种小型后台管理系统通用开发框架中的Cache缓存设计 本篇博客记录一下我在实习的公司的后台管理系统开发框架中学习到的一种关于网站的缓存&#xff08;Cache&#xff09;的实现方法&#xff0c;我会在弄懂的基础上&#xff0c;将该方法在.net core上进行实现。因为公司开发都…

前端综合笔记-JavaScript-CSS-HTML-VUE-ES6-Typescript-axios浏览器与web安全 --从入门到入坟

文章为个人整合的笔记&#xff0c;并无商业用途 点进来之后你的噩梦就要来了&#xff0c;接下来是我从业以来整理的一些基础与难点(多数来源于阅读文章与实践中遇到的问题)&#xff0c;如果你 是个小白&#xff1a; 推荐使用2~3周的时间来消化接下来的内容&#xff0c; 遇到不…

flex布局应用与踩坑

一、预告 本文不是一篇入门的文章所有请符合以下条件的战斗人员绕道&#xff1a; 1、初学前端&#xff0c;对前端的传统布局还不是很熟悉的人 2、后端人员对前端不打算深入学习的同学 二、开篇 flex布局原本是好几个月前就一直想学习的东西&#xff0c;当时flex布局还算是比较新…

前端面试+学习笔记(HTML+CSS+JavaScript+ES6+Vue+NodeJs)

前端面试学习笔记&#xff08;HTMLCSSJavaScriptES6VueNodeJs&#xff09; 一. HTML 1. 盒子模型 是什么&#xff1a;每个元素被表示为一个矩形的盒子&#xff0c;有四个部分组成&#xff1a;内容&#xff08;content&#xff09;、内边距&#xff08;padding&#xff09;、边…

ReactNative基础篇(1)语法、布局、组件通信

•1.1语法 •ES6语法&#xff1a;ECMAScript6.0&#xff08;以下简称ES6&#xff09;是JavaScript语言的下一代标准&#xff0c;已经在2015年6月正式发布了。它的目标&#xff0c;是使得JavaScript语言可以用来编写复杂的大型应用程序&#xff0c;成为企业级开发语言 •参考网…

HTML/CSS/JS 基本语法

前端 一、HTNL1、文件结构2、文本标签&#xff08;1&#xff09;块元素&#xff1a;div&#xff08;2&#xff09;行内元素&#xff1a;span&#xff08;3&#xff09;格式标签 3、图片、音频、视频&#xff08;1&#xff09;图片&#xff08;2&#xff09;音频< audio >…

flex 弹性布局

阮一峰的网络日志 首页 档案 上一篇&#xff1a;ES6 的功能侦测库 下一篇&#xff1a;Flex 布局教程&#xff1a;实 分类&#xff1a; 开发者手册 Flex 布局教程&#xff1a;语法篇 作者&#xff1a; 阮一峰 日期&#xff1a; 2015年7月10日 感谢 腾讯课堂NEXT学院 赞助本站&…

前端可能问到的面试题(ts,js,css,es6)

1.void,null,undefined&#xff0c;never,NaN void:无返回值&#xff0c;只能赋值undefined和null null:空值&#xff0c;表示不存在 undefined&#xff1a;未定义&#xff0c;声明了但没有赋值&#xff0c;对象没有赋值的属性&#xff0c;对象没有返回值 never&#xff1a;永…

利用es6和es5的继承方式写一个简单的弹窗

本片博文受到https://blog.csdn.net/zfzhuman123/article/details/90411793的启发,es6部分代码只改了一点点,逻辑也是遵照他的思想来的,而且es5继承的部分也是用了部分es6的语法 1.首先是index.html <!DOCTYPE html> <html lang"en"> <head><m…

Flex 布局教程:语法篇

阮一峰的网络日志 首页 档案 上一篇&#xff1a;ES6 的功能侦测库 下一篇&#xff1a;Flex 布局教程&#xff1a;实 分类&#xff1a; 开发者手册 Flex 布局教程&#xff1a;语法篇 作者&#xff1a; 阮一峰 日期&#xff1a; 2015年7月10日 网页布局&#xff08;layout&am…

Python全栈:ES6标准入门和Flex入门

文章目录 1 ES6标准入门2 开发环境搭建3 ES6与 JavaScript的关系4 ES6 变量与常量4.1 变量4.2 常量 5 ES6 解构赋值6 ES6 函数、箭头函数箭头函数理解this 7 JavaScript中的面向对象编程构造函数静态方法 8 ES6中的继承9 ES6中的模块化export的使用&#xff1a;import的使用在N…

ES6语法笔记

1.多行字符串 用反引号 表示&#xff1a; abc相当于 abc 2.字符串连接时使用变量 使用${变量名}直接引用字符串变量 var name 小明; var age 20; var message 你好, ${name}, 你今年${age}岁了!; alert(message); 3.字符串方法&#xff1a;不会改变原有字符串的内容&a…

mt4交易平台哪个好?不妨试试福瑞斯外汇平台

对于从事外汇交易的人来说&#xff0c;肯定对MT4不会陌生&#xff0c;众所周知&#xff0c;MT4是全球外汇交易中&#xff0c;最被广泛应用于零售客户市场的交易软件。界面简洁&#xff0c;容易上手等都是MT4潜在的优势&#xff0c;新手上路还得看MT4的指导。不过外汇交易中&…

AJPFX告诉你MT4平台有什么优势?

FX TERMINAL&#xff08;Meta Trader4&#xff09;交易平台功能 成功驾驭金融市场的第一步是拥有正确的工具。AJPFX为客户提供二十四小时的在线交易服务&#xff0c;MT4交易软件是目前全世界上最为先进&#xff0c;应用最为广泛的交易系统。客户的所要了解的行情报价&#xff0…

MT4行情交易API接口开发手记

之前开发的外汇量化交易系统&#xff0c;行情和交易接口都是通过在MT4平台下编写EA来实现&#xff0c;具体方法是&#xff1a;1、用C编写一个动态库文件&#xff0c;在里面实现行情和交易数据调用接口&#xff0c;将报价数据和K线数据写入数据库中&#xff0c;并从数据库中获取…

中期国际:MT4数据挖掘与分析方法:以数据为导向,制定有效的交易策略

在金融市场中&#xff0c;制定有效的交易策略是成功交易的关键。而要制定一份可靠的交易策略&#xff0c;数据挖掘与分析方法是不可或缺的工具。本文将介绍如何以数据为导向&#xff0c;利用MT4进行数据挖掘与分析&#xff0c;从而制定有效的交易策略。 首先&#xff0c;我们需…

双线macd指标参数最佳设置_手机mt4平台怎么使用macd双线指标

macd双线指标在外汇操作中具有重要作用&#xff0c;它既可用于电脑版的mt4&#xff0c;亦可用于手机mt4。那么&#xff0c;您对手机mt4macd双线指标了解多少&#xff1f;您知道它该如何添加、怎么使用吗&#xff1f;下文将为大家进行详细介绍。 手机mt4macd双线指标添加 手机mt…

MT4 API 跟单交易接口更新

1、MT4 API交易接口是什么&#xff1f; Api接口是跨平台多账号交易接口&#xff0c;是将MT4交易通道以API的方式聚合在一起&#xff0c;帮助开发商在各经纪商不提供manager后台账号、无须EA插件的情况下&#xff0c;也能轻松接入不同的MT4交易平台&#xff0c;完成登录、交易和…

MT4跟单软件更新至v4.23.0——HOOKSWORK多帐户跨平台

2022年12月7日更新内容&#xff1a; 1、新增“软件启动开始跟单” 当重启电脑或软件时&#xff0c;软件启动时&#xff0c;软件可自动启动开始按钮。 1&#xff09;重启服务器后自动登录系统命令&#xff1a;开始-运行- control userpasswords2 regedit>计算机\HKEY_LOCAL…