npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

custom-link

v1.0.1

Published

自定制react-router中的Link组件

Downloads

5

Readme

CustomLink

概述

CustomLink是对react-router-dom 下面插件Link的扩展,添加了属性render,以及添加了方法isPathEqual,写它的目的如下:

  1. 我想要连续多次点击某个Link的时候,路由的访问记录不要每次都保存到history中,只保留第一次的就好了
  2. link我不想写死为一个a标签,我希望其是任意元素或者是任意组合元素,触发方式是任意事件
  3. 我顺便在demo中写了一个不通过Link的事件来切换路由的办法,我觉得大部分项目中都可能是用得到的

使用示例

import React from "react";
import { render } from "react-dom";
import {Router, Route} from "react-router-dom";
import {createBrowserHistory} from "history";
import CustomLink from "custom-link";
import isHistoryPathEqual from "ishistorypathequal";
const customHistory = createBrowserHistory();
const CustomLinkExample = () => {
    return (
        <div>
            <Router history={customHistory}>
                <div>
                    <ul>
                        <li>
                            <CustomLink render={(context, props) => {
                                const { router } = context;
                                const { history } = router;
                                const { location } = history;
                                return (<button onClick={() => {
                                    const to = "/";
                                    if (!isHistoryPathEqual(to, location)) {
                                        history.push(to);
                                    } else {
                                        /* //打开这个注释,那么每次都会重新刷新对应的路由组件,否则当连续访问同一个路由则只刷新一次
                                        history.replace(to); */
                                    }
                                }}>Home</button>)
                            }} />
                        </li>
                        <li>
                            <CustomLink render={(context, props) => {
                                const to = "/about";
                                const { router } = context;
                                const { history } = router;
                                const { location } = history;
                                return (<button onClick={() => {
                                    if (!isHistoryPathEqual(to, location)) {
                                        history.push(to);
                                    } else {
                                        /* //打开这个注释,那么每次都会重新刷新对应的路由组件,否则当连续访问同一个路由则只刷新一次
                                        history.replace(to); */
                                    }
                                }}>About</button>)
                            }} />
                        </li>
                    </ul>
                    <hr />

                    <Route exact path="/" component={Home} />
                    <Route path="/about" component={About} />
                </div>
            </Router>
            <button onClick={() => {
                const to = "/about";
                const { location } = customHistory;
                if (!isHistoryPathEqual(to, location)) {
                    customHistory.push(to);
                } else {
                    /* //打开这个注释,那么每次都会重新刷新对应的路由组件,否则当连续访问同一个路由则只刷新一次
                    customHistory.replace(to); */
                }
            }}>跳转到about页</button>
        </div>

    )
}
const Home = () => (
    <div>
        home
    </div>
)
const About = () => {
    console.log("refresh");
    return (
        <div>
            about
        </div>
    )
}
render(<CustomLinkExample />, document.querySelector("#example"));

simple