react & vue & angular

关于react-router

2022-12-03  本文已影响0人  涅槃快乐是金

一、先问两个问题


二、react-router是怎么跳转的

  1. react-router的一般用法
    import { HashRouter, Route, Switch } from 'react-router-dom';
    <HashRouter>
      <Switch>
        <Route path='/' component={Home}/>
        <Route path='/list' render={() => <List />}/>
    <Route path='/detail'><Detail /><Route>
      </Switch>
    </HashRouter>
    
  2. react-router-dom和react-router什么关系
    看下react-router目录:
    export { default as MemoryRouter } from "./MemoryRouter.js";
    export { default as Prompt } from "./Prompt.js";
    export { default as Redirect } from "./Redirect.js";
    export { default as Route } from "./Route.js";
    export { default as Router } from "./Router.js";
    export { default as StaticRouter } from "./StaticRouter.js";
    export { default as Switch } from "./Switch.js";
    export { default as generatePath } from "./generatePath.js";
    export { default as matchPath } from "./matchPath.js";
    export { default as withRouter } from "./withRouter.js";
    
    export { default as __HistoryContext } from "./HistoryContext.js";
    export { default as __RouterContext } from "./RouterContext.js";
    
    export { useHistory, useLocation, useParams, useRouteMatch } from "./hooks.js";
    

react-router-dom目录:

export {
  MemoryRouter,
  Prompt,
  Redirect,
  Route,
  Router,
  StaticRouter,
  Switch,
  generatePath,
  matchPath,
  withRouter,
  useHistory,
  useLocation,
  useParams,
  useRouteMatch
} from "react-router";

export { default as BrowserRouter } from "./BrowserRouter.js";
export { default as HashRouter } from "./HashRouter.js";
export { default as Link } from "./Link.js";
export { default as NavLink } from "./NavLink.js";

2.1 BrowserRouter和HashRouter

import { Router } from "react-router";
import { createHashHistory as createHistory } from "history";
class HashRouter extends React.Component {
  history = createHistory(this.props);

  render() {
    return <Router history={this.history} children={this.props.children} />;
  }
}

2.2 Router

<RouterContext.Provider
        value={{
          history: this.props.history,
          location: this.state.location,
          match: Router.computeRootMatch(this.state.location.pathname),
          staticContext: this.props.staticContext
        }}
      >
        <HistoryContext.Provider
          children={this.props.children || null}
          value={this.props.history}
        />
      </RouterContext.Provider>

2.3 Switch

<RouterContext.Consumer>
        {context => {
          invariant(context, "You should not use <Switch> outside a <Router>");
          const location = this.props.location || context.location;
          let element, match;

          // We use React.Children.forEach instead of React.Children.toArray().find()
          // here because toArray adds keys to all child elements and we do not want
          // to trigger an unmount/remount for two <Route>s that render the same
          // component at different URLs.
          React.Children.forEach(this.props.children, child => {
            if (match == null && React.isValidElement(child)) {
              element = child;
              const path = child.props.path || child.props.from;
              match = path
                ? matchPath(location.pathname, { ...child.props, path })
                : context.match;
            }
          });
          return match
            ? React.cloneElement(element, { location, computedMatch: match })
            : null;
        }}
      </RouterContext.Consumer>

2.4 Route(children, component, render 三种render方式)

<RouterContext.Consumer>
        {context => {
          invariant(context, "You should not use <Route> outside a <Router>");

          const location = this.props.location || context.location;
          const match = this.props.computedMatch
            ? this.props.computedMatch // <Switch> already computed the match for us
            : this.props.path
            ? matchPath(location.pathname, this.props)
            : context.match;

          const props = { ...context, location, match };

          let { children, component, render } = this.props;

          // Preact uses an empty array as children by
          // default, so use null if that's the case.
          if (Array.isArray(children) && isEmptyChildren(children)) {
            children = null;
          }

          return (
            <RouterContext.Provider value={props}>
              {props.match
                ? children
                  ? typeof children === "function"
                    ? __DEV__
                      ? evalChildrenDev(children, props, this.props.path)
                      : children(props)
                    : children
                  : component
                  ? React.createElement(component, props)
                  : render
                  ? render(props)
                  : null
                : typeof children === "function"
                ? __DEV__
                  ? evalChildrenDev(children, props, this.props.path)
                  : children(props)
                : null}
            </RouterContext.Provider>
          );
        }}
      </RouterContext.Consumer>

从源码我们可以看到:

上一篇 下一篇

猜你喜欢

热点阅读