• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

ipfs-shipyard/react-ipfs-url: Grab a URL from a IPFS path by using URL.createObj ...

原作者: [db:作者] 来自: 网络 收藏 邀请

开源软件名称:

ipfs-shipyard/react-ipfs-url

开源软件地址:

https://github.com/ipfs-shipyard/react-ipfs-url

开源编程语言:

JavaScript 100.0%

开源软件介绍:

react-ipfs-url

NPM version Downloads Build Status Coverage Status Dependency status Dev Dependency status

Grab a URL from a IPFS path by using URL.createObjectURL.

Installation

$ npm install react-ipfs-url

This library is written in modern JavaScript and is published in both CommonJS and ES module transpiled variants. If you target older browsers please make sure to transpile accordingly.

Usage

With <IpfsUrl> component:

import React from 'react';
import { IpfsUrl } from 'react-ipfs-url';

const ipfs = /* your ipfs node, perhaps provide it via context */;

const SomeComponent = () => (
    <IpfsUrl ipfs={ ipfs } input="/ipfs/QmQuMzeovz...">
        { ({ status, value }) => (
            <>
                { status === 'pending' && 'Loading...' }
                { status === 'rejected' && 'Oops, failed to load' }
                { status === 'fulfilled' && <img src={ value } alt="" /> }
            <>
        ) }
    </IpfsUrl>
);

With useIpfsUrl() hook:

import React from 'react';
import { useIpfsUrl } from 'react-ipfs-url';

const ipfs = /* your ipfs node, perhaps provide it via context */;

const SomeComponent = () => {
    const [urlStatus, urlValue] = useIpfsUrl(ipfs, '/ipfs/QmQuMzeovz..');

    return (
        <>
            { status === 'pending' && 'Loading...' }
            { status === 'rejected' && 'Oops, failed to load' }
            { status === 'fulfilled' && <img src={ value } alt="" /> }
        </>
    );
};

API

IpfsUrl

The <IpfsUrl> component allows you to conditionally render children based on the url status and fulfillment/rejection value. It leverages the render props technique to know what to render.

Props

All properties from react-promiseful are also valid.

ipfs

Type: object

The ipfs node to be used.

input

Type: string

A valid IPFS path, hash or a provider URL, such as a gateway URL or an Infura URL.

⚠️ At the moment, IPNS paths are not supported for the ipfs and ipfsOffline providers: #2

⚠️ There's no support for fully qualified domains URLs yet: #4

strategy

Type: string
Default: input-first

The strategy to use when resolving a valid URL.

  • input-first: Use the provider associated with the input first and fallback to resolving with IPFS.
  • ipfs-first: Use IPFS to resolve the URL first and fallback to resolving with the provider associated with the input
  • ipfs-offline-first: Use offline IPFS first and fallback to resolving with the provider, followed by the online IPFS.
  • ipfs-only: Only use IPFS to resolve the URL, even if input comes from another provider
  • ipfs-offline-only: Only use IPFS (offline) to resolve the URL, even if input comes from another provider
checkTimeout

Type: object
Default: { gateway: 12500, infura: 12500, ipfsOffline: 5000, ipfs: 180000 }

The max time to spend checking for the existence of the content on providers.

disposeDelayMs

Type: number
Default: 60000

The delay in which object urls created with URL.createObjectURL are revoked when they are no longer used.

children

Type: Function

A render prop function with the following signature:

(state) => {}

The state argument is an object that contains the following properties:

  • status is one of none (when there's no promise), pending, rejected, fulfilled
  • value is either the fulfillment value (url) or the rejection value
  • withinThreshold indicating if we are still within the configured thresholdMs

See react-promiseful for more info.

useIpfsUrl(ipfs, input, [options])

The hook version of the <IpfsUrl> component. The options available to both are exactly the same.

const urlState = useIpfsUrl(ipfs, '/ipfs/QmQuMzeovz..');

The returned value from the hook is the url promise state, an object that contains the following properties:

  • status is one of none (when there's no promise), pending, rejected, fulfilled
  • value is either the fulfillment value (url) or the rejection value
  • withinThreshold indicating if we are still within the configured thresholdMs

See react-promiseful for more info.

Tests

$ npm test
$ npm test -- --watch # during development

License

Released under the MIT License.




鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
dignifiedquire/anno: Mirandor meets IPFS发布时间:2022-06-22
下一篇:
cxgreat2014/IPFS_MovieOnline: ZeroNet上的在线影院发布时间:2022-06-22
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap