Giter VIP home page Giter VIP logo

react-treasure's Introduction

React Treasure

React Treasure is a tool that allows you to share and store data across React components. It uses the React Context API to manage data.

Installation

npm install react-treasure

Usage

Before you can use the Treasure you need to wrap your application/parent component with the TreasureProvider.

import { Provider } from 'react-treasure';

const defaultUser = {
  name: 'John Doe',
  age: 20
};

function App() {
  return (
    <Provider initialData={{ user: defaultUser }}>
      <User />
    </Provider>
  );
}

Here let's say we have a User component that has a UserDetails component as a child. We want to share the user data across both components.

import React from 'react';
import { useTreasure } from 'react-treasure';

const User = () => {
  const [userData] = useTreasure('user');

  return (
    <div>
      <h1>{user.name}</h1>
      <UserDetails />
    </div>
  );
};

In the UserDetails component we want to access the user data and modify it.

import React from 'react';
import { useTreasure } from 'react-treasure';

const UserDetails = () => {
  const [user, setUser] = useTreasure('user');

  return (
    <div>
      <button onClick={() => setUser({ name: 'Jeff Bezos', age: 55 })}>Change User</button>
    </div>
  );
};

License

This project is licensed under the MIT license. See the LICENSE file for more info.

react-treasure's People

Contributors

shahradelahi avatar lemon2311 avatar

Stargazers

 avatar  avatar  avatar Dal avatar Anuja Rahul Gunasinghe avatar Josué Diogo Pedro avatar yamataku0518  avatar alireza javadi avatar Legato Bluesummers avatar Hamada avatar Daniel Mukenya  avatar Paul Koop avatar  avatar  avatar

Watchers

 avatar  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.