[React Router V4] Create Basic Routes with the React Router v4 BrowserRouter

React Router 4 has several routers built in for different purposes. The primary one you will use for building web applications is the BrowserRouter. In this lesson you will import the BrowserRouter and create some basic Route components.

After create your app with ‘creat-react-app‘, we going to install the react-router-dom:

npm i -D [email protected]

Import BrowserRouter:

import {
    BrowserRouter as Router,
    Route
} from ‘react-router-dom‘;

Using Router:

        <Router>
            <div>
                <Route exact path="/" component={App}></Route>
                <Route path="/about" component={About}></Route>
                <Route
                    strict
                    path="/about/"
                    render={() => <h2>About render</h2>}></Route>
                <Route
                    path="/demo"
                    children={({match}) => match && <h2>demo</h2>}></Route>
            </div>
        </Router>

Here we use three different ways to render a component or Html to the DOM:

1. Using Component:

                <Route exact path="/" component={App}></Route>
                <Route path="/about" component={About}></Route>

Here the ‘exact‘ flag tells that it should match only ‘/‘.

2. Using render:

we can just render some html:

                <Route
                    strict
                    path="/about/"
                    render={() => <h2>About render</h2>}></Route>

3. Using children:

                <Route
                    path="/demo"
                    children={({match}) => match && <h2>demo</h2>}></Route>

By default what we write into ‘children‘ will be rendered no matter which path it matchs.

If for example, we only want it to be shown when match ‘/demo‘, we can check whether there is a ‘match‘ object exists on props.

时间: 2024-08-07 04:32:27

[React Router V4] Create Basic Routes with the React Router v4 BrowserRouter的相关文章

react看这篇就够了(react+webpack+redux+reactRouter+sass)

本帖将对一下内容进行分享: 1.webpack环境搭建: 2.如何使用react-router: 3.引入sass预编译: 4.react 性能优化方案: 5.redux结合react使用: 6.fetch使用: 7.项目目录结构: 一.webpack配置,代码如下: 1.在根目录下新建一个webpack.config.js,这个为开发环境的webpack配置:因为得区分开发跟生产环境,所以还得新建一个webpack.production.config.js作为生产环境使用的配置文档, webp

【react自制全家桶】一、Webstrom+React+Ant Design+echarts搭建react项目

前言 一.React是Facebook推出的一个前端框架,之前被用于著名的社交媒体Instagram中,后来由于取得了不错的反响,于是Facebook决定将其开源.出身名门的React也不负众望,成功成为当前最火热的三大前端框架之一.相比于Angular,React更加轻量.而相对于另一个轻量级前端框架Vue来说,React虽然学习和使用起来难度稍微大一些,但是React的社区相对来说人气更旺,而且在移动端的开发上面,大名鼎鼎的React Native更是尽显优势,在代码性能上要好过Vue框架.

学习React前端框架,报错 &#39;React&#39; must be in scope when using JSX react/react-in-jsx-scope

问题 import react from 'react'  改成  import React from 'react'   小写 react  改成 React 学习React前端框架,报错 'React' must be in scope when using JSX react/react-in-jsx-scope 原文地址:https://www.cnblogs.com/BensonHai/p/9952156.html

react入门系列之todolist代码优化(使用react 新特性,es6语法)

代码优化 1 /** 2 - 今天我们通过es6语法,以及react新特性来优化我们的todo-list 3 - 顺带解决上个版本的key报错问题 4 */ 使用es6的解构赋值优化代码 1 /** 2 - 当我们需要一个对象某个属性的时候,我们可以使用解构赋值,这样在后续的代码就不需要通过原对象不停调用属性去获取了 3 - const { index } = this.props -----> 这就是解构赋值,在后续同一作用域使用this.props.index的时候直接使用index就可以了

react常见组件问题Can&#39;t perform a React state update on an unmounted component

在些react组件的时候,会有这个警告 Can't perform a React state update on an unmounted component. This is a no-op, but it indicates a memory leak in your application. To fix, cancel all subscriptions and asynchronous tasks in the componentWillUnmount method” 这是因为在写一

react native windows create bundle folder

生成bundle 文件 命令 react-native bundle --platform android --dev false --entry-file index.js --bundle-output android/app/src/main/assets/index.android.bundle --assets-dest android/app/src/main/res/ 原文地址:https://www.cnblogs.com/moreyear/p/8329246.html

[转] React Native Navigator?—?Navigating Like A Pro in React Native

There is a lot you can do with the React Native Navigator. Here, I will try to go over a few examples of what you can do with the Navigator and explain how it all works in depth. In React Native you have two choices as of now for Navigation (only one

[Angular2 Router] Lazy Load Angular 2 Modules with the Router

Angular 2 lazy loading is a core feature of Angular 2. Lazy loading allows your application to start up faster because it only needs to use the main App Module when the page initially loads. As you navigate between routes, it will load the additional

【REACT NATIVE 系列教程之十二】REACT NATIVE(JS/ES)与IOS(OBJECT-C)交互通信

一用到跨平台的引擎必然要有引擎与各平台原生进行交互通信的需要.那么Himi先讲解React Native与iOS之间的通信交互. 本篇主要分为两部分讲解:(关于其中讲解的OC语法等不介绍,不懂的请自行学习) 1. React Native 访问iOS 2. iOS访问React Native     一:React Native 访问iOS 1. 我们想要JS调用OC函数,就要实现一个"RCTBridgeModule"协议的Objective-C类 所以首先我们先创建一个oc新类,