react基础(jsx,组件,react-dom,react-router-dom,red ux,脚手架安装+新旧生命周期+通信...)

概念

          react官网: https://reactjs.org/

React UI组件库:  

           material-ui(国外):官网: http://www.material-ui.com/#/

  ant-design(国内蚂蚁金服):官网: https://ant.design/index-cn

 react 是目前最流行的框架;其中是采用 mvvm 的思想,让我们把所有的只关注视图层和逻辑层

 react 的创始人:Facebook; js 的创始人:布兰登 艾奇;

 react由: (jsx,组件,react-dom),和他的全家桶技术(react-router-dom,redux)

         jsx:用于将jsx转化js语法      react-router-dom:路由

          react-dom:虚拟dom            redux:管理数据,相当于vue中的vuex
prop-types:我们想要对语法进行验证,及限制数据类型和默认值
            static defaultProps={ //设置props默认值
               title:'hello word'
              }
            static propTypes={ //设置props的数据类型
                  title:PropTypes.string.isRequired
              }

react-router-dom:react 中实现路由跳转的插件

redux:用于组件之间进行通信,但是我们写中大型项目的话,无疑是最好的选择,小项目的话,可以不考虑

基础使用:

 

       三部分:1.导入三个包, 2.准备盒子 ,3.在babel中操作数据 

   需要引入三个文件:react.development.js,react-dom.development.js,babel.min.js,

  react.development.js:react核心库   

  react-dom.development.js:扩展库用于支持操作dom

  babel.min.js: 用于将jsx转化js语法

react脚手架安装 :

    1. 全局安装: npm i create-react-app -g

    2. 新建目录: create-react-app   mypro(项目名)

    3.进入目录:  cd mpro(项目名)

    4.运行:         npm start

事件的 处理写法:

 ref 的使用(获取元素节点)

 

for 循环,及 if 条件判断

 

新旧生命周期对比:

 旧的生命周期:

 

1. 组件的三个生命周期状态:
	Mount:插入真实 DOM
	Update:被重新渲染
	Unmount:被移出真实 DOM
2. 生命周期流程:
	* 第一次初始化显示: ReactDOM.render(<Xxx/>, containDom)
		constructor()
		componentWillMount() : 将要插入回调
		render() : 用于插入虚拟DOM回调
		componentDidMount() : 已经插入回调
	* 每次更新state: this.setState({})
	    componentWillReceiveProps(): 接收父组件新的属性
	    componentWillUpdate() : 将要更新回调
	    render() : 更新(重新渲染)
	    componentDidUpdate() : 已经更新回调
	* 删除组件: ReactDOM.unmountComponentAtNode(div): 移除组件
		componentWillUnmount() : 组件将要被移除回调
3. 常用的方法
	render(): 必须重写, 返回一个自定义的虚拟DOM
  	constructor(): 初始化状态, 绑定this(可以箭头函数代替)
  	componentDidMount() : 只执行一次, 已经在dom树中, 适合启动/设置一些监听

新生命周期:

 

          //卸载组件按钮的回调
			death = ()=>{
				ReactDOM.unmountComponentAtNode(document.getElementById('test'))
			}

			//强制更新按钮的回调
			force = ()=>{
				this.forceUpdate()
			}
			
			//若state的值在任何时候都取决于props,那么可以使用getDerivedStateFromProps
			static getDerivedStateFromProps(props,state){
				console.log('getDerivedStateFromProps',props,state);
				return null
			}

			//在更新之前获取快照
			getSnapshotBeforeUpdate(){
				console.log('getSnapshotBeforeUpdate');
				return 'atguigu'
			}

			//组件挂载完毕的钩子
			componentDidMount(){
				console.log('Count---componentDidMount');
			}

			//组件将要卸载的钩子
			componentWillUnmount(){
				console.log('Count---componentWillUnmount');
			}

			//控制组件更新的“阀门”
			shouldComponentUpdate(){
				console.log('Count---shouldComponentUpdate');
				return true
			}

			//组件更新完毕的钩子
		componentDidUpdate(preProps,preState,snapshotValue){
			console.log('Count---  componentDidUpdate',preProps,preState,snapshotValue);
                  
			}

react通信

      在父组件中的子组件标签上绑定属性: <Child name={this.state.name}

      在子组件中通过props接收: this.props.name

 

        

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
react-router-domReact框架中用于处理路由的库,它提供了一种方便的方式来管理应用程序的不同页面之间的导航和状态。而import.meta.glob是ES模块的一个新特性,它可以用于动态地导入模块。 在react-router-dom 6中,import.meta.glob可以用于动态地导入路由组件。它可以根据指定的模式匹配文件路径,并将匹配到的文件作为路由组件进行导入。这样可以方便地实现按需加载路由组件,提高应用程序的性能和加载速度。 具体使用方法如下: 1. 首先,在项目中安装react-router-dom 6: ``` npm install react-router-dom@next ``` 2. 在需要使用import.meta.glob的地方,使用如下语法进行导入: ```jsx import { lazy } from 'react'; import { BrowserRouter as Router, Route, Routes } from 'react-router-dom'; const routes = import.meta.glob('/path/to/routes/*.jsx'); function App() { return ( <Router> <Routes> {Object.entries(routes).map(([path, component]) => { const routePath = path.replace('/path/to/routes', '').replace('.jsx', ''); const LazyComponent = lazy(component); return <Route key={routePath} path={routePath} element={<LazyComponent />} />; })} </Routes> </Router> ); } export default App; ``` 在上面的代码中,`import.meta.glob('/path/to/routes/*.jsx')`会根据指定的模式匹配`/path/to/routes/`目录下的所有`.jsx`文件,并返回一个对象,其中键是文件路径,值是对应的模块。 然后,我们使用`Object.entries(routes)`将对象转换为数组,并使用`map`方法遍历数组,生成对应的`Route`组件。在遍历过程中,我们使用`lazy`函数将路由组件进行懒加载,以实现按需加载的效果。 这样,我们就可以根据文件路径动态地导入路由组件,并在应用程序中进行路由配置。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值