[PReact] Handle Simple Routing with preact-router
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的更多相关文章
- Preact(React)核心原理详解
原创: 宝丁 玄说前端 本文作者:字节跳动 - 宝丁 一.Preact 是什么 二.Preact 和 React 的区别有哪些? 三.Preact 是怎么工作的 四.结合实际组件了解整体渲染流程 五. ...
- Routing Manager for WCF4 z
http://www.codeproject.com/Articles/77198/Routing-Manager-for-WCF Download source Contents Features ...
- PCI Express(六) - Simple transactions
原文地址:http://www.fpga4fun.com/PCI-Express6.html Let's try to control LEDs from the PCI Express bus. X ...
- Akka源码分析-Router
akak中还有一个比较重要的概念,那就是Router(路由).路由的概念,相信大家都不陌生,在akka中,它就是其他actors的一个代理,会把消息按照路由规则,分发给指定的actor.我一般喜欢把R ...
- AKKA Router路由
路由概念 大量的actor在并行工作的时候,处理到来的消息流,这时候就需要一个组件或者东西来引导消息从源到目的地Actor,这个组件或者东西就是Router在Akka中,router也是一种actor ...
- 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 ...
- nodejs开发 express路由与中间件
路由 通常HTTP URL的格式是这样的: http://host[:port][path] http表示协议. host表示主机. port为端口,可选字段,不提供时默认为80. path指定请求资 ...
- The main concepts
The MVC application model A Play application follows the MVC architectural pattern applied to the we ...
- 转:分享13款PHP开发框架
文章来自于:http://mashable.com/2014/04/04/php-frameworks-build-applications/ Building software applicatio ...
随机推荐
- uname 命令
uname -p 显示系统的芯片类型.如,PowerPC uname -r 显示操作系统的版本号 uname -s 显示系统名称.例如,AIX uname -n 显示节点名称 uname -a 显示系 ...
- NYOJ_75 日期计算 (推断这一天是这一年中的第几天)
题目地址 如题,输入一个日期,格式如:2010 10 24 ,推断这一天是这一年中的第几天. 分析: 官方给的最优答案用了for 和switch语句结合,十分巧妙. 代码 /* 如题,输入一个日期 ...
- AC自己主动机模板
AC自己主动机模板-- /* * AC自己主动机模板 * 用法: * 1.init() : 初始化函数 * 2.insert(str) : 插入字符串函数 * 3.build() : 构建ac自己主动 ...
- 【直接拿来用のandroid公共代码模块解析与分享】の Notification和NotificationManager
本文源代码托管在https://github.com/ASCE1885/asce-common,欢迎fork Android项目做得多了.会发现原来非常多基础的东西都是能够复用,这个系列介绍一些自己项 ...
- Wiz+360云盘,让你的知识库井井有条
用了wiz快两年了,一些同事看到我在找资料时打开wiz,总会好奇的问这是什么,想到还有很多同仁在用文件夹管理知识库,于是想分享一下我的管理方法.(PS:鄙人愚见,如有高见,望指教) Wiz为知笔记下载 ...
- weblogic虚拟路径配置
首发地址 https://blog.leapmie.com/archives/344/ 前言 weblogic的虚拟路径配置有两种: 一种是在项目下配置,即在weblogic.xml中配置,该方法配置 ...
- Shiro学习总结(4)——Shrio登陆验证实例详细解读
最终效果如下: 工程整体的目录如下: Java代码如下: 配置文件如下: 页面资源如下: 好了,下面来简单说下过程吧! 准备工作: 先建表: [sql] view plain copy drop ta ...
- ubuntu下useradd与adduser差别,新建用户不再home文件夹下
useradd username不会在/home下建立一个目录username adduser username会在/home下建立一个目录username useradd -m username跟a ...
- js无缝滚动原理及详解[转自刹那芳华]
刚刚接触JS,网上找了一些关于无缝滚动的教程,但都大同小异,对我这种新手来说也只是会用,不知道什么意思,想要自己写个更是一头雾水.于是找了一些资料,详细说明一下JS无缝滚动的原理,相信看过这篇文章之后 ...
- python,寻找班级里面名字最长的人
寻找班级里面名字最长的人 我有一串字符串人名:names=(' Kunpen Ji, Li XIAO, Caron Li,' ' Dongjian SHI, Ji ZHAO, Fia YUAN Y,' ...