Some applications only need a very minimal routing solution. This lesson will cover a practical example showing the router in use. We’ll build a simple search feature that accepts user input and then calls the github API. We’ll see how to access route parameters, how to manually & automatically navigate around, and finally how to handle un-matched path. https://github.com/developit/preact-router

Install:

  1. npm install --save preact-router

Define routers:

  1. import {h} from 'preact';
  2. import { Router } from 'preact-router';
  3. import Profile from './Profile';
  4. import Home from './Home';
  5. import Error from './Error';
  6.  
  7. export default function App() {
  8. return (
  9. <Router>
  10. <Home path="/" />
  11. <Profile path="/profile/:user"/>
  12. <Error default/>
  13. </Router>
  14. );
  15. }

Defailt Error router:

  1. import {h} from 'preact';
  2. import {route} from 'preact-router';
  3.  
  4. const back = (e) => {
  5. route('/');
  6. };
  7.  
  8. export default Error = () => (
  9. <div>
  10. <h2>Error!</h2>
  11. <button onClick={e => back(e)}>Home</button>
  12. </div>
  13. );

Home: preact call route() function to navigate between components.

  1. import { h } from 'preact';
  2. import { route } from 'preact-router';
  3.  
  4. function search(query) {
  5. route(`/profile/${encodeURIComponent(query)}`);
  6. }
  7.  
  8. export default function Home() {
  9. return (
  10. <section>
  11. <p>Enter a Github Username</p>
  12. <input type="search"
  13. placeholder="username"
  14. onSearch={e => search(e.target.value)}
  15. />
  16. </section>
  17. );
  18. }

Profile.js: Stateful component, fetching data:

  1. import {h, Component} from 'preact';
  2. import User from './User';
  3.  
  4. const config = {
  5. url: 'https://api.github.com/users'
  6. };
  7.  
  8. export default class Profile extends Component {
  9. constructor(props) {
  10. super(props);
  11.  
  12. this.state = {
  13. loading: true,
  14. user: null
  15. };
  16. }
  17.  
  18. componentDidMount() {
  19. fetch(`${config.url}/${this.props.user}`)
  20. .then(resp => resp.json())
  21. .then(user => {
  22. this.setState({
  23. user,
  24. loading: false
  25. });
  26. })
  27. .catch(err => console.error(err));
  28. }
  29.  
  30. render({user: username}, {loading, user: userState}) {
  31. return (
  32. <div class="app">
  33. {loading
  34. ? <p>Fetching {username}'s profile</p>
  35. : <User image={userState.avatar_url}
  36. name={userState.name} />
  37. }
  38. </div>
  39. );
  40. }
  41. }

[PReact] Handle Simple Routing with preact-router的更多相关文章

  1. Preact(React)核心原理详解

    原创: 宝丁 玄说前端 本文作者:字节跳动 - 宝丁 一.Preact 是什么 二.Preact 和 React 的区别有哪些? 三.Preact 是怎么工作的 四.结合实际组件了解整体渲染流程 五. ...

  2. Routing Manager for WCF4 z

    http://www.codeproject.com/Articles/77198/Routing-Manager-for-WCF Download source Contents Features ...

  3. PCI Express(六) - Simple transactions

    原文地址:http://www.fpga4fun.com/PCI-Express6.html Let's try to control LEDs from the PCI Express bus. X ...

  4. Akka源码分析-Router

    akak中还有一个比较重要的概念,那就是Router(路由).路由的概念,相信大家都不陌生,在akka中,它就是其他actors的一个代理,会把消息按照路由规则,分发给指定的actor.我一般喜欢把R ...

  5. AKKA Router路由

    路由概念 大量的actor在并行工作的时候,处理到来的消息流,这时候就需要一个组件或者东西来引导消息从源到目的地Actor,这个组件或者东西就是Router在Akka中,router也是一种actor ...

  6. Endless looping of packets in TCP/IP networks (Routing Loops)

    How endless looping of packets in a TCP/IP network might occur? Router is a device used to interconn ...

  7. nodejs开发 express路由与中间件

    路由 通常HTTP URL的格式是这样的: http://host[:port][path] http表示协议. host表示主机. port为端口,可选字段,不提供时默认为80. path指定请求资 ...

  8. The main concepts

    The MVC application model A Play application follows the MVC architectural pattern applied to the we ...

  9. 转:分享13款PHP开发框架

    文章来自于:http://mashable.com/2014/04/04/php-frameworks-build-applications/ Building software applicatio ...

随机推荐

  1. uname 命令

    uname -p 显示系统的芯片类型.如,PowerPC uname -r 显示操作系统的版本号 uname -s 显示系统名称.例如,AIX uname -n 显示节点名称 uname -a 显示系 ...

  2. NYOJ_75 日期计算 (推断这一天是这一年中的第几天)

    题目地址 如题,输入一个日期,格式如:2010 10 24 ,推断这一天是这一年中的第几天. 分析:   官方给的最优答案用了for 和switch语句结合,十分巧妙. 代码 /* 如题,输入一个日期 ...

  3. AC自己主动机模板

    AC自己主动机模板-- /* * AC自己主动机模板 * 用法: * 1.init() : 初始化函数 * 2.insert(str) : 插入字符串函数 * 3.build() : 构建ac自己主动 ...

  4. 【直接拿来用のandroid公共代码模块解析与分享】の Notification和NotificationManager

    本文源代码托管在https://github.com/ASCE1885/asce-common,欢迎fork Android项目做得多了.会发现原来非常多基础的东西都是能够复用,这个系列介绍一些自己项 ...

  5. Wiz+360云盘,让你的知识库井井有条

    用了wiz快两年了,一些同事看到我在找资料时打开wiz,总会好奇的问这是什么,想到还有很多同仁在用文件夹管理知识库,于是想分享一下我的管理方法.(PS:鄙人愚见,如有高见,望指教) Wiz为知笔记下载 ...

  6. weblogic虚拟路径配置

    首发地址 https://blog.leapmie.com/archives/344/ 前言 weblogic的虚拟路径配置有两种: 一种是在项目下配置,即在weblogic.xml中配置,该方法配置 ...

  7. Shiro学习总结(4)——Shrio登陆验证实例详细解读

    最终效果如下: 工程整体的目录如下: Java代码如下: 配置文件如下: 页面资源如下: 好了,下面来简单说下过程吧! 准备工作: 先建表: [sql] view plain copy drop ta ...

  8. ubuntu下useradd与adduser差别,新建用户不再home文件夹下

    useradd username不会在/home下建立一个目录username adduser username会在/home下建立一个目录username useradd -m username跟a ...

  9. js无缝滚动原理及详解[转自刹那芳华]

    刚刚接触JS,网上找了一些关于无缝滚动的教程,但都大同小异,对我这种新手来说也只是会用,不知道什么意思,想要自己写个更是一头雾水.于是找了一些资料,详细说明一下JS无缝滚动的原理,相信看过这篇文章之后 ...

  10. python,寻找班级里面名字最长的人

    寻找班级里面名字最长的人 我有一串字符串人名:names=(' Kunpen Ji, Li XIAO, Caron Li,' ' Dongjian SHI, Ji ZHAO, Fia YUAN Y,' ...