[Preact] Integrate react-router with Preact

React-router is the community favourite routing solution - it can handle all of your complex routing needs and in this lesson we’ll cover what’s needed to enable it’s use within Preact. https://github.com/ReactTraining/react-router

in webpack.config.js:

    resolve: {
        alias: {
            ‘react‘: ‘preact-compat‘,
            ‘react-deom‘: ‘preact-compat‘
        }
    },

Add resolve block. it alias ‘react‘ to use ‘preact-compat‘.

Change Route definations.

import {h} from ‘preact‘;
import { BrowserRouter as Router, Switch, Route } from ‘react-router-dom‘;
import Profile from ‘./Profile‘;
import Home from ‘./Home‘;
import Error from ‘./Error‘;

export default function App() {
    return (
        <Router>
            <Switch>
                <Route path=‘/‘ component={Home} exact />
                <Route path=‘/profile/:user‘ component={Profile} />
                <Route component={Error}></Route>
            </Switch>
        </Router>
    );
}

Using ‘Switch‘ to allow only one component showing at a time.

Dealing with navigation:

import { h } from ‘preact‘;
import {withRouter} from ‘react-router-dom‘;

function search(router, query) {
    router.history.push(`/profile/${encodeURIComponent(query)}`);
}

const Home = withRouter((router) => {
    return (
        <section>
            <p>Enter a Github Username</p>
            <input type="search"
                   placeholder="username"
                   onSearch={e => search(router, e.target.value)}
            />
        </section>
    );
});

export default Home;

We can use High Order component ‘withRouter‘, it inject ‘router‘ param into our component, then we can use:

router.history.push(`/profile/${encodeURIComponent(query)}`);

to nav around.

Get router params:

    componentDidMount() {
        const username = this.props.match.params.user;
        fetch(`${config.url}/${username}`)
            .then(resp => resp.json())
            .then(user => {
                this.setState({
                                  user,
                                  loading: false
                              });
            })
            .catch(err => console.error(err));
    }

You can get the router param by using:

const username = this.props.match.params.user;

Link tag:

import {h} from ‘preact‘;
import {Link} from ‘react-router-dom‘;

export default Error = () => (
    <div>
        <h2>Error!</h2>
        <Link to=‘/‘>Home</Link>
    </div>
);
时间: 2024-10-03 00:25:14

[Preact] Integrate react-router with Preact的相关文章

react router 4.0以上的路由应用

thead>tr>th{padding:8px;line-height:1.4285714;border-top:1px solid #ddd}.table>thead>tr>td,.table>tbody>tr>th,.table>tbody>tr>td,.table>tfoot>tr>th,.table>tfoot>tr>td{padding:8px;line-height:1.4285714;ver

最新的chart 聊天功能( webpack2 + react + router + redux + scss + nodejs + express + mysql + es6/7)

请表明转载链接:http://www.cnblogs.com/zhangkunweb/p/6853728.html 我是一个喜欢捣腾的人,没事总喜欢学点新东西,可能现在用不到,但是不保证下一刻用不到. 我一直从事的是依赖angular.js 的web开发,但是我怎么能一直用它呢?看看最近火的一塌糊涂的reactjs ,我的天啊,不学会它,怎么能睡好觉. 今天我分享一个依赖最新版本的webpack + react + router + redux + scss + nodejs + mysql +

[React Router v4] Use the React Router v4 Link Component for Navigation Between Routes

If you’ve created several Routes within your application, you will also want to be able to navigate between them. React Router supplies a Link component that you will use to make this happen. Import Link: import { BrowserRouter as Router, Route, Link

[React Router V4] Create Basic Routes with the React Router v4 BrowserRouter

React Router 4 has several routers built in for different purposes. The primary one you will use for building web applications is the BrowserRouter. In this lesson you will import the BrowserRouter and create some basic Route components. After create

[Redux] Filtering Redux State with React Router Params

We will learn how adding React Router shifts the balance of responsibilities, and how the components can use both at the same time. Now when we click the filter, the url changes, but the todos list doesn't change. So continue with that. Router only r

[Redux] Navigating with React Router &lt;Link&gt;

We will learn how to change the address bar using a component from React Router. In Root.js: We need to add a param to change the Route: import React from 'react'; import {Provider} from 'react-redux'; import {Router, Route, browserHistory } from 're

[Redux] Adding React Router to the Project

We will learn how to add React Router to a Redux project and make it render our root component. Install: npm install --save react-router import React from 'react'; import {Provider} from 'react-redux'; import {Router, Route} from 'react-router'; impo

[转] React Router 使用教程

你会发现,它不是一个库,也不是一个框架,而是一个庞大的体系.想要发挥它的威力,整个技术栈都要配合它改造.你要学习一整套解决方案,从后端到前端,都是全新的做法. 举例来说,React 不使用 HTML,而使用 JSX .它打算抛弃 DOM,要求开发者不要使用任何 DOM 方法.它甚至还抛弃了 SQL ,自己发明了一套查询语言 GraphQL .当然,这些你都可以不用,React 照样运行,但是就发挥不出它的最大威力. 这样说吧,你只要用了 React,就会发现合理的选择就是,采用它的整个技术栈.

React漫漫学习路之 React Router

React Router 是一个基于 React 之上的强大路由库,它可以让你向应用中快速地添加视图和数据流,同时保持页面与 URL 间的同步. 目前react-router最新版本已经到4.0+,因为新的版本是一次非常大的改动,所以这里直接讨论4.0以上版本. 引用 react-router // React Router 核心 react-router-dom // 用于 DOM 绑定的 React Router react-router-native // 用于 React Native

Redux+React Router+Node.js全栈开发

详情请交流  QQ  709639943 01.Java深入微服务原理改造房产销售平台 02.跨平台混编框架 MUI 仿豆瓣电影 APP 03.Node.js入门到企业Web开发中的应用 04.Redux+React Router+Node.js全栈开发 05.Java秒杀系统方案优化 高性能高并发实战 06.企业级刚需Nginx入门,全面掌握Nginx配置+快速搭建高可用架构 07.快速上手Linux 玩转典型应用 08.全面系统讲解CSS 工作应用+面试一步搞定 09.Java Spring