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. mysql查询最新一组数据

    public function getDetail(){ $sql = "SELECT * FROM (SELECT * FROM Z_CFTC_GOV_NEW ORDER BY UPDAT ...

  2. VMware虚拟机XP系统安装

    转载:http://jingyan.baidu.com/article/54b6b9c00e2f452d593b4762.html

  3. JAVASE学习笔记:第十章 SWing经常使用控件类(二)

    7.JComboBox 下拉列表         /*   * 初始化下拉列表   */  public void addcomb(){   String[] area = {"山西省&qu ...

  4. <memory>(包括了auto_ptr,shared_ptr等各种指针)

    Memory elements This header defines general utilities to manage dynamic memory: Allocators allocator ...

  5. HDU 2333 Assemble(二分)

    Assemble Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Su ...

  6. codeforces 204E. Little Elephant and Strings(广义后缀自动机,Parent树)

    传送门在这里. 大意: 给一堆字符串,询问每个字符串有多少子串在所有字符串中出现K次以上. 解题思路: 这种子串问题一定要见后缀自动机Parent树Dfs序统计出现次数都是套路了吧. 这道题统计子串个 ...

  7. Unity5中的粒子缩放(附测试源码)

    本文章由cartzhang编写,转载请注明出处. 所有权利保留. 文章链接:http://blog.csdn.net/cartzhang/article/details/49363241 作者:car ...

  8. C++ 与C# 对应的变量互转

    一.C++ 与C# 对应的变量互转的使用实例 C++的动态链接库的函数: C# 调用C++动态链接库数据类型的转换,其中在C++中数据类型为char *,在C#中对应的数据类型为intPtr. 二.常 ...

  9. 有趣的Ruby-学习笔记4

    Ruby块 块.在我看来就是插入一段可变的函数 block_name{ statement1 statement2 .......... } 看起来不知道是什么,只是别急,继续往下看. 块函数通过yi ...

  10. 第三次作业 201731082208 黄亚恒&肖莉

    Github项目地址:https://github.com/HYHSTUDEY/WordCount.git 作业地址:https://www.cnblogs.com/hyhhyh090628/p/10 ...