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. WHU 1470 Join in tasks 水题

    http://acm.whu.edu.cn/land/problem/detail?problem_id=1470 大概是给你一个队列,每次移动队头的数到队尾并减1,如果本身这个数为1就删去. 然后a ...

  2. JavaScript笔记(5)

    1.return 跳出当前函数 返回一个结果 <script> //return可以跳出当前函数 所以return写在函数的最后一行 function out() { return fun ...

  3. 关于css的入门知识

    css:叠层样式表,给html添加样式的 接下来说一说,在网页中如何嵌套style样式 1.行间样式:把style(*权重1000)作为属性卸载标签里 eg:<p style="col ...

  4. 【例题 8-15 UVA - 12174】Shuffle

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 枚举第一段的范围[0..i] (0<=i<s) 然后看看[i+1..i+s-1],[i+s,i+s+s-1]..这些区间 ...

  5. Method and apparatus for transitioning between instruction sets in a processor

    A data processor (104) is described. The data processor (104) is capable of decoding and executing a ...

  6. Mindjet MindManager 思维导图软件-使用思维导图跟踪调用流程,绘制软件框架

    思维导图.据说是每一个产品经理必备的软件.假设你阅读大型源码.使用思维导图跟踪调用流程,绘制软件框架将会很方便. 特点:没什么好说的.用过的都说好. 软件截图: 下载:http://www.mindm ...

  7. IAR for STM8介绍、下载、安装与注册--转

    Ⅰ.写在前面 本文讲述的内容是IAR for STM8的介绍.下载.安装与注册,其安装.注册过程和IAR for ARM类似,如果需要了解IAR for ARM相关的文章,可以到我博客,或微信公众号查 ...

  8. RecyclerView 展示多种类型Item数据

    一.多Item布局实现(MultipleItem) 如果之前你用过ListView实现过此功能,那么你一定对下面这两个方法并不陌生 @Override public int getItemViewTy ...

  9. 如何运行vue项目(维护他人的项目)

    假如你是个小白,在公司接手他人的项目,这个时候,该怎么将这个项目跑通? 前提: 首先,这个教程主要针对vue小白,并且不知道安装node.js环境的.言归正传,下面开始教程:在维护项目之前,需要把所有 ...

  10. geotools修改shapefile 属性名乱码问题

    在GeoServer中文社区的讨论地址为:http://opengeo.cn/bbs/read.php?tid=1701&page=e&#a 使用geotools修改shapefile ...