npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

effector-routing

v0.1.4

Published

Simple effector router

Downloads

5

Readme

Effector Routing

Simple abstact router on top of Effector. Also has React bindings and DOM adapter

Installation

npm i effector-routing

Usage

Init router

import { addRoutes } from "effector-routing"
import { initDomRouter } from "effector-routing/dist/dom"

const routes = {
  home: {
    view: HomePage,
    meta: {
      path: "/"
    }
  },
  posts: {
    view: PostsList,
    meta: {
      path: "/posts"
    }
  },
  singlePost: {
    view: SinglePost,
    meta: {
      path: "/posts/:id"
    }
  }
}

addRoutes(routes)
initDomRouter({
  defaultRoute: { name: "home" }
})

Navigation, stores and events

import {
  $route,
  goTo,
  historyBack,
  beforeRouteEnter,
  routeChanged,
  onRouteChanged
} from "effector-routing"

// Imperative navigation
// Both functions are Effects
goTo({ name: "posts" })
goTo({ name: "singlePost", params: { id: 1 } })
historyBack()

// $route is a Store which contains current { name, params }
// So you can .map, .watch, combine etc
const $postId = $route.map(({ name, params }) =>
  name === "singlePost" ? params.id : null
)

// Add a middleware
beforeRouteEnter(({ name, params }) => {
  // Navigate "as is"
  if (name !== "singlePost") {
    return
  }

  // Change route
  if (params.id === 2) {
    return {
      name: "singlePost",
      params: { id: 1 }
    }
  }

  // Undo navigation
  if (params.id === 3) {
    return false
  }
})

// Call something on route change
onRouteChanged(({ name, params }) => {
  console.log({ name, params })
})

// Also available as an Event
routeChanged.watch(({ name, params }) => {
  console.log({ name, params })
})

Use with React

Components

import React from 'react'
import { RouteLink, RouterView } from "effector-routing/dist/react"

const Menu = () => {
  return <nav>
    <RouteLink name="home">Home</RouteLink>
    <RouteLink name="posts">Posts</RouteLink>
    <RouteLink name="singlePost" params={{id:1}}>View Post (ID: 1)</RouterLink>
  </nav>
}

const App = () => {
  return (
    <div>
      <Menu />
      <RouterView />
    </div>
  )
}

Example with useStore

import React from "react"
import { combine } from "effector"
import { useStore } from "effector-react"

import { $route } from "effector-routing"
import { $postsList } from "./stores"

const $postId = $route.map(({ params }) => params.id)

const $currentPost = combine($postId, $postsList, (id, list) =>
  list.find(post => post.id === id)
)

const Post = () => {
  const currentPost = useStore($currentPost)

  return (
    <article>
      <h1>{currentPost.title}</h1>
      <p>{currentPost.description}</p>
    </article>
  )
}

Writing your own adapter

If you want to write your own adapter
Here's an example of adapter which stores last route in LocalStorage (e.g. for Electron)

import { initFirstRoute, onRouteChanged } from "effector-routing"

export const initLsRouter = ({ defaultRoute }) => {
  let lastRoute = defaultRoute
  try {
    lastRoute = JSON.parse(localStorage.lastRoute)
  } catch (err) {
    lastRoute = defaultRoute
  }

  initFirstRoute(defaultRoute)
  onRouteChanged(newRoute => {
    localStorage.lastRoute = JSON.stringify({
      name: newRoute.name,
      params: newRoute.params
    })
  })
}

And just use it then

import { addRoutes } from "effector-routing"
import { initLsRouter } from "./adapter"

const routes = {
  /* ... */
}

addRoutes(routes)
initLsRouter()