Marcin Jahn | Dev Notebook
  • Home
  • Programming
  • Technologies
  • Projects
  • About
  • Home
  • Programming
  • Technologies
  • Projects
  • About
  • An icon of the Core section Core
    • Programs Execution
    • Stack and Heap
    • Asynchronous Programming
      • Overview
      • Event Queues
      • Fibers
      • Stackless Coroutines
  • An icon of the .NET section .NET
    • HTTPClient
    • Async
      • How Async Works
      • TAP Tips
    • Equality
    • Comparisons
    • Enumerables
    • Unit Tests
    • Generic Host
    • Logging
    • Configuration
    • Records
    • Nullability
    • Garbage Collector
    • IL and Allocations
    • gRPC
    • Source Generators
    • Platform Invoke
    • ASP.NET Core
      • Overview
      • Middleware
      • Razor Pages
      • Routing in Razor Pages
      • Web APIs
      • Filters
      • Identity
      • Validation
      • Tips
    • Entity Framework Core
      • Overview
      • Testing
      • Tips
  • An icon of the Angular section Angular
    • Overview
    • Components
    • Directives
    • Services and DI
    • Routing
    • Observables (RxJS)
    • Forms
    • Pipes
    • HTTP
    • Modules
    • NgRx
    • Angular Universal
    • Tips
    • Standalone Components
  • An icon of the JavaScript section JavaScript
    • OOP
    • JavaScript - The Weird Parts
    • JS Functions
    • ES Modules
    • Node.js
    • Axios Tips
    • TypeScript
      • TypeScript Environment Setup
      • TypeScript Tips
    • React
      • React Routing
      • MobX
    • Advanced Vue.js Features
  • An icon of the Rust section Rust
    • Overview
    • Cargo
    • Basics
    • Ownership
    • Structures
    • Enums
    • Organization
    • Collections
    • Error Handling
    • Generics
    • Traits
    • Lifetimes
    • Closures
    • Raw Pointers
    • Smart Pointers
    • Concurrency
    • Testing
    • Tips
  • An icon of the C/C++ section C/C++
    • Compilation
    • Structures
    • OOP in C
    • Pointers
    • Strings
    • Dynamic Memory
    • argc and argv Visualization
  • An icon of the GTK section GTK
    • Overview
    • GObject
    • GJS
  • An icon of the CSS section CSS
    • Responsive Design
    • CSS Tips
    • CSS Pixel
  • An icon of the Unity section Unity
    • Unity
  • An icon of the Functional Programming section Functional Programming
    • Fundamentals of Functional Programming
    • .NET Functional Features
    • Signatures
    • Function Composition
    • Error Handling
    • Partial Application
    • Modularity
    • Category Theory
      • Overview
      • Monoid
      • Other Magmas
      • Functors
  • An icon of the Algorithms section Algorithms
    • Big O Notation
    • Array
    • Linked List
    • Queue
    • Hash Table and Set
    • Tree
    • Sorting
    • Searching
  • An icon of the Architecture section Architecture
    • What is architecture?
    • Domain-Driven Design
    • ASP.NET Core Projects
  • An icon of the Core section Core
    • Programs Execution
    • Stack and Heap
    • Asynchronous Programming
      • Overview
      • Event Queues
      • Fibers
      • Stackless Coroutines
  • An icon of the .NET section .NET
    • HTTPClient
    • Async
      • How Async Works
      • TAP Tips
    • Equality
    • Comparisons
    • Enumerables
    • Unit Tests
    • Generic Host
    • Logging
    • Configuration
    • Records
    • Nullability
    • Garbage Collector
    • IL and Allocations
    • gRPC
    • Source Generators
    • Platform Invoke
    • ASP.NET Core
      • Overview
      • Middleware
      • Razor Pages
      • Routing in Razor Pages
      • Web APIs
      • Filters
      • Identity
      • Validation
      • Tips
    • Entity Framework Core
      • Overview
      • Testing
      • Tips
  • An icon of the Angular section Angular
    • Overview
    • Components
    • Directives
    • Services and DI
    • Routing
    • Observables (RxJS)
    • Forms
    • Pipes
    • HTTP
    • Modules
    • NgRx
    • Angular Universal
    • Tips
    • Standalone Components
  • An icon of the JavaScript section JavaScript
    • OOP
    • JavaScript - The Weird Parts
    • JS Functions
    • ES Modules
    • Node.js
    • Axios Tips
    • TypeScript
      • TypeScript Environment Setup
      • TypeScript Tips
    • React
      • React Routing
      • MobX
    • Advanced Vue.js Features
  • An icon of the Rust section Rust
    • Overview
    • Cargo
    • Basics
    • Ownership
    • Structures
    • Enums
    • Organization
    • Collections
    • Error Handling
    • Generics
    • Traits
    • Lifetimes
    • Closures
    • Raw Pointers
    • Smart Pointers
    • Concurrency
    • Testing
    • Tips
  • An icon of the C/C++ section C/C++
    • Compilation
    • Structures
    • OOP in C
    • Pointers
    • Strings
    • Dynamic Memory
    • argc and argv Visualization
  • An icon of the GTK section GTK
    • Overview
    • GObject
    • GJS
  • An icon of the CSS section CSS
    • Responsive Design
    • CSS Tips
    • CSS Pixel
  • An icon of the Unity section Unity
    • Unity
  • An icon of the Functional Programming section Functional Programming
    • Fundamentals of Functional Programming
    • .NET Functional Features
    • Signatures
    • Function Composition
    • Error Handling
    • Partial Application
    • Modularity
    • Category Theory
      • Overview
      • Monoid
      • Other Magmas
      • Functors
  • An icon of the Algorithms section Algorithms
    • Big O Notation
    • Array
    • Linked List
    • Queue
    • Hash Table and Set
    • Tree
    • Sorting
    • Searching
  • An icon of the Architecture section Architecture
    • What is architecture?
    • Domain-Driven Design
    • ASP.NET Core Projects

React Routing

Packages

Terminal window
npm i react-router-dom
npm install @types/react-router-dom

Usage

Enable routing

In src/index.ts:

ReactDOM.render(
<BrowserRouter>
<App />
</BrowserRouter>,
document.getElementById("root")
);

Add routing

In App.tsx:

<Container style={{ marginTop: "7em" }}>
<Route exact path="/" component={HomePage} />
<Route path="/activities" component={ActivityDashboard} />
<Route path="/activities/:id" component={ActivityDetails} />
<Route path="/createActivity" component={ActivityForm} />
</Container>

React-router displays the first route which satisfies the path, even aprtially, that’s why adding exact parameter to the first route is needed.

The third route has a path parameter “id”.

Conditional rendering

Sometimes we not only want to render some component in fixed place based on routing, but possibly we want to display different page layout based on routing. We can then use Route inside of another Route:

return (
<Fragment>
<Route exact path="/" component={HomePage} />
<Route path="/(.+)" render={() => (
<Fragment>
<NavBar />
<Container style={{ marginTop: "7em" }}>
<Route exact path="/activities" component={ActivityDashboard} />
<Route path="/activities/:id" component={ActivityDetails} />
<Route
key={location.key}
path={["/createActivity", "/manage/:id"]}
component={ActivityForm}
/>
</Container>
</Fragment>
)}
/>
</Fragment>
);

if we navigate to / we will display ONLY HomePage. If we navigate to anything else, we will display appropriate component under a NavBar. path="/(.+)" checks if there is anything after /.

Loading just one route

It might happen that a few routes satisfy the path. We might use Swicth component around our routes to make sure that only one route is loaded.

<Switch>
<Route exact path="/activities" component={ActivityDashboard} />
<Route path="/activities/:id" component={ActivityDetails} />
<Route
key={location.key}
path={["/createActivity", "/manage/:id"]}
component={ActivityForm}
/>
<Route component={NotFound} />
</Switch>

It’s also a good practice to have a route to “Not Found” page at the end. It’s without a path, because it should “catch” anything that wasn’t caught by previous routes.

Navigation using components

There are two components to be used:

  • Link
  • NavLink - like Link, but can add styling

Sometimes it’s useful to rended other components as Link. Example:

<Menu.Item name="Activities" as={Link} to="/activities" />

Menu.Item (Semantic UI somponent) will appear as normal, however it will become a link to “/activities”.

With NavLink, “active” clas will be set automatically when clicked on a Menu.Item (class name can be changed, for Semantic UI “active” is correct).

<Menu.Item header as={NavLink} exact to="/">

Again, exact property is needed if we want to highlight only the correct thing.

Route props

When we use router, components have access to additional props, which have various functionalities, i.e. it shows path parameters:

If we want to use some custom path parameters that our component is supposed to receive in URL (like id), we need to create interface for contents of params:

interface IDetailParams {
id: string;
}

Component then needs to be typed. RouteComponentProps is generic:

const ActivityDetails: React.FC<RouteComponentProps<IDetailParams>>

IDetailParams should contain data that will be in match.params.

Navigation using code

One of the props we get is history. It has various methods like goBack(), goForward(), push(), etc. We can use those to navigate.

<Button
content="Cancel"
onClick={() => history.push('/activities')}
/>

Adding router props to root component

If a component was not render by router, we can add routing additions (additional props like history), by using withRoute higher order function:

export default withRouter(observer(App));

In this case we’re also using observer from MobX.

Other tips

Router keeps same scrollbar position when routing to different pages. It can be undesired.

MobX  →
© 2023 Marcin Jahn | Dev Notebook | All Rights Reserved. | Built with Astro.