GithubHelp home page GithubHelp logo

dtrucs / react-ctx-connect Goto Github PK

View Code? Open in Web Editor NEW

This project forked from hadrienl/react-ctx-connect

0.0 0.0 0.0 1.52 MB

A tool to connect component to a React context like react-redux but better

JavaScript 98.35% Shell 1.65%

react-ctx-connect's Introduction

react-ctx-connect

This tool helps you to connect your React component to a React context. It will let you set props you want to get from your context and inject to your component and avoid useless re-renders.

Install

$ npm i react-ctx-connect

Usage

context.js

import React from 'react';
import connect from 'react-ctx-connect';

export const context = React.createContext({ foo: 'Hello World' });

export const connectContext = connect(context);

Component/Component.js

import React from 'react';

export default ({ foo }) => <p>{foo}</p>

Component/index.js

import { connectContext } from './context.js';
import Component from './Component';

export default connectContext('foo')(Component);

Advanced Usage

There is three ways to access context values :

List of strings

You can pass a list of props names as strings for an exact match between context values and component props.

connectContext('foo', 'bar')(Component);

Component will have foo and bar props with context.foo and context.bar values.

Mapping

You can specify an object of key/values to rename your props.

connectContext({
  foo: 'someValue',
  bar: 'someOtherValue',
})(Component);

Component will have foo prop with context.someValue value and bar prop with context.someOtherValue value.

You can also specify a path if you want to access a deep value in your context.

connectContext({
  foo: 'some.value',
})(Component);

Function

Finally, you can create a function to access the current context value and return you own object of props.

connectContext(context => ({
  foo: context.some.value,
}))(Component);
connectContext(({ some: { value: foo }}) => ({ foo }))(Component);

In theses two examples, Component will have a foo prop with context.some.value value.

Tests

$ npm test

Author

Hadrien Lanneau

react-ctx-connect's People

Contributors

dependabot[bot] avatar hadrienl avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.