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:

npm install --save preact-router

Define routers:

import {h} from 'preact';
import { Router } from 'preact-router';
import Profile from './Profile';
import Home from './Home';
import Error from './Error'; export default function App() {
return (
<Router>
<Home path="/" />
<Profile path="/profile/:user"/>
<Error default/>
</Router>
);
}

Defailt Error router:

import {h} from 'preact';
import {route} from 'preact-router'; const back = (e) => {
route('/');
}; export default Error = () => (
<div>
<h2>Error!</h2>
<button onClick={e => back(e)}>Home</button>
</div>
);

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

import { h } from 'preact';
import { route } from 'preact-router'; function search(query) {
route(`/profile/${encodeURIComponent(query)}`);
} export default function Home() {
return (
<section>
<p>Enter a Github Username</p>
<input type="search"
placeholder="username"
onSearch={e => search(e.target.value)}
/>
</section>
);
}

Profile.js: Stateful component, fetching data:

import {h, Component} from 'preact';
import User from './User'; const config = {
url: 'https://api.github.com/users'
}; export default class Profile extends Component {
constructor(props) {
super(props); this.state = {
loading: true,
user: null
};
} componentDidMount() {
fetch(`${config.url}/${this.props.user}`)
.then(resp => resp.json())
.then(user => {
this.setState({
user,
loading: false
});
})
.catch(err => console.error(err));
} render({user: username}, {loading, user: userState}) {
return (
<div class="app">
{loading
? <p>Fetching {username}'s profile</p>
: <User image={userState.avatar_url}
name={userState.name} />
}
</div>
);
}
}

[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. module.exports,exports和export default,export的区别

    前提:CommonJS模块规范和ES6模块规范是完全不同的两个概念. module.exports,exports属于CommonJS模块规范: export default,export属于ES6模 ...

  2. VS Code 终端显示问题

    一.打开编辑器的终端时候,然后弹出了系统自带的cmd窗口 解决办法: Win+R 输入cmd 打开windows cmd窗口,窗口顶部右键属性,然后取消勾选使用旧版控制台,然后重启编辑器就行了. 二. ...

  3. 【UML】UML在软件开发各个阶段的应用

    一.UML5个互联视图 UML中经常使用5个互联的视图来描写叙述系统的体系结构. 如图 (1)用例视图(Use-case View) 由专门描写叙述可被终于用户.分析人员.測试人员看到的系统行为的用例 ...

  4. vmware-虚拟机播放器的下载、安装

    如果是在window下安装的话: 1.下载vmware: 到官网下载免费个人版本 https://my.vmware.com/cn/web/vmware/free#desktop_end_user_c ...

  5. elasticsearch的master选举机制

    master作为cluster的灵魂必须要有,还必须要唯一,否则集群就出大问题了.因此master选举在cluster分析中尤为重要.对于这个问题我将分两篇来分析.第一篇也就是本篇,首先会简单说一说m ...

  6. 72.挖掘CSDN密码到链表并统计密码出现次数生成密码库

    list.h #define _CRT_SECURE_NO_WARNINGS #include <stdio.h> #include <stdlib.h> #include & ...

  7. Codeforces 718C. Sasha and Array(线段树)

    传送门 解题思路: 这道题给了我们一个崭新的角度来看线段树. 我们常常使用的线段树是维护区间的函数的. 这里呢,提示我们线段树其实还可以维护递推. 美好的矩阵递推性质支持了这一功能. 或者说,对于递推 ...

  8. java和javascript日期校验和闰年问题分析和解决方式

    1.闰年的介绍 地球绕太阳执行周期为365天5小时48分46秒(合365.24219天)即一回归年.公历的平年仅仅有365日,比回归年短约0.2422 日,所余下的时间约为四年累计一天.故四年于2月加 ...

  9. 不重新启动VMWare虚拟机加入虚拟磁盘的方法(上)

    近期因为业务须要在不重新启动系统的前提下对系统进行扩容,前提是该系统做过lvm.可是没有足够的物理卷(硬盘),所以引出了改文.本文共分为上下两部分.这是第一部分. 文件夹 加入磁盘 做LVM 加入硬盘 ...

  10. 7.3 GROUP BY的“新”功能

    7.3 GROUP BY的"新"功能正在更新内容,请稍后