百科.dev
全部条目在线工具AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
R

react-tunnel

> 编程语言
开源

用于将上下文类似的值放入上下文中并将其作为属性提取出来的 React 组件和装饰器

99 stars0 点赞1 次浏览
访问官网GitHub

工具介绍

用于将上下文类似的值放入上下文中并将其作为属性提取出来的 React 组件和装饰器

react-tunnel

React components and decorators for putting context-like values into context and pulling them out as props

Heavily copied/modeled off the code used in react-redux by @gaearon

Table of Contents

  • Installation
  • React Native
  • Quick Start
  • Best Practices
  • API
    • ``
    • inject([mapProvidedToProps])
  • Thanks
  • License

installation

npm install --save react-tunnel.

React Native

for React: require/import from react-tunnel.
For React Native: require/import from react-tunnel/native.

Quick Start

react-tunnel helps you provide injectable props to child components to help avoid deep chains of prop passing

  • install with npm install react-tunnel
  • import or require Provider and inject by
javascript

//es5
var Provider = require('react-tunnel').Provider;
var inject = require('react-tunnel').inject;

//es6
import { Provider, inject } from 'react-tunnel'
  • wrap a Component tree with `` like
javascript
//using object provide
render() {
    return (
        
          
        
    )
}

//or as a function
function provider () {
  return {
    thing: "one",
    anotherThing: 2g
  }
}

render() {
    return (
        
          
        
    )
}
  • decorate a child component with inject and provide a mapping function determine which provided props to inject into the decorated component
javascript
var SomeChild = require('./SomeChildOfAnything') //a react Component

function mapProvidedToProps(provided) {
    return {
        that: provided.thing
    }
}

//notice that inject returns a wrapping function
var InjectedChild = inject(mapProvidedToProps)(SomeChild); 

// now in InjectedChild props will have `that`
...
render() {
    var injectedProp = this.props.that;
    return {injectedProp};
    //will render as one
}

Best Practices

react-tunnel uses React's context feature to make provided props available to children regardless of how deep they are. While this is powerful it also can be abused and make for a nightmare to manage.

It is reccommended that this functionality be used to provide generally static properties that don't change much if at all based on the local conditions of the injecting component. Examples might include

  • Providing viewport dimensions to arbitrarily deep Components
  • Providing redux action creators from a parent connected Component to deep children

Also please consider that the context api for React has PropType checking for a reason and that by using this library and opting out of that stronger contract has costs and you may want to utilize the base context features rather than this library

API

``

makes provide available via context.provided to children of Provider. use inject to access them easily

Props

  • provide {fn | object}:
    • provide: function(parentProvided) { return provided{object} }: will provide the return value of provide prop. Function takes in any provided values from parent providers if any. If none, an empty object is passed.
    • provide: object: provides any parent provided values if nested along with provide object properties. if there is a key collision the properties of the provide prop are used and mask similarly named properties from any parent provided objects

Nesting

Providers are nestable and if using the object version of provide will automatically reprovide any values provided in the immediate parent Provider. Use this if you want to say Provide some truly global props at the root of your App but also use Providers for Redux action creators produced via connect to the local render tree.

If you nest Providers but use the function form of provide you will need to forward any desired parent provided values using the function forms argument parentProvided.

inject([mapProvidedToProps])

Creates a decorator which injects props from Provider into the decorated component according to the mapProvidedToProps function.

Arguments

  • mapProvidedToProps(provided)? returns object: called when decorated Component mounts and when it receives new context. the return object of this call is added to the underlying Component as props
  • default: if mapProvidedToProps is not passed to inject then all Provider values are passed to underlying component.

Thanks

  • @gaearon for inspiring this API with the more specialized react-redux
  • @rt2zz for helping flesh out the design and API

License

MIT

Issues· 0 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

JavaScript

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月18日
分类编程语言
定价开源

> 相关工具

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言