首页 > 其他分享 >react react-router-dom6 mobx6整理

react react-router-dom6 mobx6整理

时间:2022-11-07 10:34:19浏览次数:61  
标签:index const react store import router dom6

react react-router-dom v6 

route/index.js

import React, { Suspense, lazy } from 'react'
import Layout from '../views/Layout/index'
const Home = lazy(() => import('../views/home/index'))
const List = lazy(() => import('../views/list/index'))
const Test = lazy(() => import('../views/test/index'))
const Detail = lazy(() => import('../views/detail/index'))
const lazyLoad = (children) => {
    return <Suspense fallback={<div>loading……</div>}>
        {children}
    </Suspense>
}
const router = [
    {
        path: "/",
        element: <Layout />,
        children: [
            {
                index: true,
                element: lazyLoad(<Home />)
            },
            {
                path: 'list/:id',
                element: lazyLoad(<List />)
            },
            {
                path: 'detail',
                element: lazyLoad(<Detail />)
            },
            // {
            //     path: 'detail/:id',
            //     element: lazyLoad(<Detail />)
            // },
            {
                path: 'test',
                element: lazyLoad(<Test />)
            }
        ]
    }
];

export default router

app.js
import { useRoutes } from "react-router-dom";
import Router from './route/index'
const App3 = () => {
    return useRoutes(Router)
}

export default App3

index.js
import React from 'react';
import ReactDOM from 'react-dom/client';
import { ConfigProvider } from 'antd';
import zhCN from 'antd/es/locale/zh_CN';
import moment from 'moment';
import 'moment/locale/zh-cn';
import './index.less';
import App from './App3';
import { BrowserRouter } from "react-router-dom";
moment.locale('zh-cn');

const root = ReactDOM.createRoot(document.getElementById('root'));
root.render(
    <ConfigProvider locale={zhCN}>
      <BrowserRouter>
          <App />
      </BrowserRouter>
       
    </ConfigProvider>
);
路由跳转传参
  import { useParams, useNavigate, useSearchParams, useLocation } from "react-router-dom";
  Navigate('/detail/'+4);  
  //接受参数 const params = useParams(); 
 Navigate('/detail?age='+39);
 //接受参数  const [search] = useSearchParams();  console.log('search', search.get('age'))
 Navigate('/detail', {
        state: {
              id: 6
        }
   });
 // 接受参数  const location = useLocation()   console.log('location',location.state.id);

 // mobx6使用
 // store/mobx2Test.js
 import { action, makeObservable, observable } from "mobx"
 
export default class CounterStore {
  constructor() {
    this.count = 0
    makeObservable(this, {
      count: observable,
    //   increment: action,    
    //   decrement: action,
      increment: action.bound,    // bound 改变 组件中 this 指向
      decrement: action.bound
    })
  }
  increment() {
    this.count += 1
  }
  decrement() {
    this.count -= 1
  }
}
 // list/list.js
 import React, { useEffect, useState } from 'react';

 import Counter  from './Counter'
 import { useParams, useNavigate } from "react-router-dom";
 import CounterStore from '../../store/mobx2Test'
 const counterStore = new CounterStore();
 <Counter store = { counterStore }></Counter>
// list/Counter.js

import { observer } from "mobx-react";

function Counter ({ store }) {
    // 改变this指向这样可以写
    const { count, increment, decrement } = store; 
  return (
    <div>
      <button onClick={ () => increment() }>+</button>
      <span>{ count }</span>
      <button onClick={ () => decrement() }>-</button>
       如果store中没改变this指向用地下这种
      {/* <button onClick={ () => store.increment() }>+</button>
      <span>{ store.count }</span>
      <button onClick={ () => store.decrement() }>-</button> */}
    </div>
  )
}

export default observer(Counter);

// 父元素调用子组件间方法

import { createRef, forwardRef, useRef, useImperativeHandle } from 'react';

const Foo = forwardRef((params, inputRef1) => {
  const inputRef = useRef();
  // 主要这个useImperativeHandle钩子    
  useImperativeHandle(inputRef1, () => ({
    focus1 () {
        console.log(3333333333)
      //EEinputRef.current.focus();
    }
  }));

  return <input type="text" ref={ inputRef } />;
});

const App = () => {
  const inputRef = useRef();

  const onClick = () => {
    inputRef.current.focus1();
  }

  return <div>
    <Foo ref={ inputRef } />
    <button onClick={ onClick }>button</button>
  </div>;
}
export default App

 

标签:index,const,react,store,import,router,dom6
From: https://www.cnblogs.com/whlBooK/p/16865123.html

相关文章

  • React组件基础二
    1.注册事件React注册事件与DOM的事件语法非常像语法on+事件名={事件处理程序}比如onClick={this.handleClick}注意:React事件采用驼峰命名法,比如onMouseEnter,onClick......
  • React 全家桶-React基础
    React全家桶-React基础用于构建用户界面的JavaScript库。facebook开源、组件化、声明式编码、ReactNative移动端开发、虚拟DOM+Diffing算法官网:https://react.docschi......
  • webstorm里面react快速创建模板
    webstorm里面react快速创建模板rcc+tab键--用ES6模块系统创建一个React组件类rccp+tab键--创建一个带有PropTypes和ES6模块系统的React组件类rcfc+tab键-......
  • react 利用history.blcok实现路由跳转拦截
    欢迎关注前端早茶,与广东靓仔携手共同进阶​​​​前端早茶专注前端,一起结伴同行,紧跟业界发展步伐~ 一、前言“history对象”指的是history软件包,它是ReactRouter仅有的......
  • react 利用history.blcok实现路由跳转拦截
    欢迎关注前端早茶,与广东靓仔携手共同进阶​​​​前端早茶专注前端,一起结伴同行,紧跟业界发展步伐~ 一、前言“history对象”指的是history软件包,它是ReactRouter仅有的......
  • 前端vue2+vue-router3+ElementUI+axios综合使用代码教程登录页案例
    为了测试vue2+vue-router3路由+ElementUI界面+axios网络HTTP请求的基本使用情况专门编写一个登录页面进行demo验证  依赖情况package.json{"name":"default",......
  • React使用Antd自定义主题报错
    安装包"customize-cra":"^1.0.0","customize-cra-less-loader":"^2.0.0","less":"^4.1.3","less-loader":"^11.1.0",修改config-overrides.jsconst{override......
  • 解决react执行两遍的问题
    解决react执行两遍的问题原因使用脚手架创建项目后默认会开启严格模式,在严格模式下,React开发环境下会刻意执行两次渲染,用于突出显示应用程序中潜在问题。解决将入......
  • React Native for Arcgis 地图开发 影像RasterLayer(十)
    RasterLayerCtrl的封装调用importReact,{useState,useEffect}from‘react’;import{View,Text,ScrollView,StyleSheet}from‘react-native’;import{Button,......
  • React Native for Arcgis 地图开发 GraphicCtrl (十三)
    GraphicCtrl的使用importReact,{useState,useEffect}from‘react’;import{View,Text,ScrollView,StyleSheet}from‘react-native’;import{Button,List,S......