All Projects → frejs → use-routes

frejs / use-routes

Licence: other
🚩 A router hook for not only react hooks API but also fre

Programming Languages

javascript
184084 projects - #8 most used programming language
HTML
75241 projects

Projects that are alternatives of or similar to use-routes

vue-ssr-lessons
包教会的 vue 服务端渲染课程
Stars: ✭ 81 (+131.43%)
Mutual labels:  vue-router
vue3-docs
vue中文社区,vue3 中文文档
Stars: ✭ 180 (+414.29%)
Mutual labels:  vue-router
vue-admin-work
🎉🎉🚀🚀🚀🚀vue-admin-work是一个中后台系统管理方案。使用 vue2.x 及周边全家桶工具开发而来。支持多种功能,不同角色权限🚀🚀🚀🎉🎉
Stars: ✭ 74 (+111.43%)
Mutual labels:  vue-router
Vue-CAMP
VueJS
Stars: ✭ 16 (-54.29%)
Mutual labels:  vue-router
vue-seo-friendly-spa-template
Vue.js PWA/SPA template initially scaffolded with vue-cli and configured for SEO. Makes use of prerendering and other techniques/packages in order to achieve a perfect "Lighthouse Score".
Stars: ✭ 41 (+17.14%)
Mutual labels:  vue-router
shopping-cart
A vue.js project for shopping cart.
Stars: ✭ 57 (+62.86%)
Mutual labels:  vue-router
vue-mall
vue企业级商城练手项目并且提供接口唷
Stars: ✭ 14 (-60%)
Mutual labels:  vue-router
friendly-vue-starter
Full featured Vue.js starter project for browser apps
Stars: ✭ 37 (+5.71%)
Mutual labels:  vue-router
meal-ticket
前端基于webpack+vue+vux,后端基于express+lowdb。有了这些,开发一个项目将会变得很轻松。
Stars: ✭ 19 (-45.71%)
Mutual labels:  vue-router
vue-loopback
A Vue project template with Loopback framework optionally with Vuex, Vue-router, and Auth boilerplaite
Stars: ✭ 52 (+48.57%)
Mutual labels:  vue-router
vue-automation
一款开箱即用的 Vue 项目模版,基于 Vue 2.x
Stars: ✭ 31 (-11.43%)
Mutual labels:  vue-router
Laravel-Vue-SPA-template
Template for Single Page Applications built with Laravel and Vue.
Stars: ✭ 22 (-37.14%)
Mutual labels:  vue-router
mall-app-vant
有来商城youlai-mall移动APP端,技术栈:Vant,已计划变更使用uni-app适配Android、IOS端开发。
Stars: ✭ 33 (-5.71%)
Mutual labels:  vue-router
vue-webpack-boilerplate
A webpack boilerplate with vue-loader, axios, vue-router and vuex
Stars: ✭ 51 (+45.71%)
Mutual labels:  vue-router
mmf-blog-vue2-pwa-ssr
mmf-blog-vue2 ssr(The service side rendering)
Stars: ✭ 65 (+85.71%)
Mutual labels:  vue-router
Vue2.x-mobileSystem
基于Vue2.0的移动端项目,项目没有使用vue-cli,全部手写,让小白更容易学习理解
Stars: ✭ 72 (+105.71%)
Mutual labels:  vue-router
vue3-chat
2021👨‍🎓Vue2/3全家桶 + Koa+Socket+Vant3前后端分离移动端聊天应用。vue+node全栈入门项目
Stars: ✭ 46 (+31.43%)
Mutual labels:  vue-router
vue-boilerplate
Vue boilerplate
Stars: ✭ 28 (-20%)
Mutual labels:  vue-router
vue-jsPlumb
jsPlumb with Vue 2.【WIP】
Stars: ✭ 66 (+88.57%)
Mutual labels:  vue-router
megalo-router-patch
在 megalo 中使用 vue-router 的路由写法
Stars: ✭ 15 (-57.14%)
Mutual labels:  vue-router

use-routes

800 Bytes router hook for Fre and React

Use

import { h, render } from 'fre'
import { useRoutes, push } from 'use-routes'

const routes = {
  '/': () => (
    <div>
      <p>home</p>
      <button onClick={() => push('/home/jack')}>Go jack</button>
    </div>
  ),
  '/home/:id': ({ id }) => (
    <div>
      <p>{id}</p>
      <button onClick={() => push('/')}>Go home</button>
    </div>
  ),
}

const App = () => useRoutes(routes)

render(<App />, document.getElementById('root'))

以上,首先定义一个路由的对象,key 为正则路径,value 为组件

history 模式

React

use-routes 同时适用于 fre 和 react,但默认是服务于 fre 的

如果想要用于 react,可以通过 webpack 配置别名

resolve: {
  alias: {
    'fre': 'react'
  }
}
import React from 'react'
import { useRoutes } from 'use-routes'

function App(){
  const result = useRoutes(routes)
  return result || <NotFound />
}

useRoutes

function App(){
  const result = useRoutes(routes)
  return result || <NotFound />
}

useRoutes 返回一个组件,这个组件会根据 pathname 和 routes 的 key 匹配

如果匹配不到,可以事先准备一个全局的 404 组件 √

redirect

重定向,只需要将 routes 对象的 value 变成需要重定向的 路径字符串 即可

const routes = {
  '/': '/home',
  '/home': Home,
}

如上,当匹配到跟路径,会跳转到 home 路径,并渲染 home 组件

A link

方便跳转,默认也对外暴露了一个 A 组件,作用和 Link 组件一样:

<A href='/home/jack'>Go jack</A>

标签属性和 a 标签一样,只是默认做了阻止冒泡

License

MIT ©132yse, inspired by routerhook

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].