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

drizzle-react-context

v2.0.0

Published

Put some Drizzle on your React components.

Downloads

4

Readme

drizzle-react

Requires React 0.14+ npm install --save drizzle-react

drizzle-react is the official way to integrate Drizzle with your React dapp.

Tired of constantly coding contract calls after your state changes? Wish you had a one-liner for knowing when your dapp is ready to use? Ethereum developers have to account for extra considerations that traditional apps don't have to worry about. Drizzle abstracts away the boilerplate of creating a dapp front-end, allowing you to focus on what makes it unique. Drizzle handles instantiating web3 and contracts, fetching accounts, and keeping all of this data in sync with the blockchain.

Check out the Drizzle Truffle Box for a complete example, or continue reading to create your own setup.

Getting Started

Note: Since Drizzle uses web3 1.0 and web sockets, be sure your development environment can support these.

  1. Import the provider.

    import { DrizzleProvider } from 'drizzle-react'
  2. Create an options object and pass in the desired contract artifacts for Drizzle to instantiate. Other options are available, see the Options section of the Drizzle docs below.

    // Import contracts
    import SimpleStorage from './../build/contracts/SimpleStorage.json'
    import TutorialToken from './../build/contracts/TutorialToken.json'
    
    const options = {
      contracts: [
        SimpleStorage,
        TutorialToken
      ]
    }
  3. Wrap your app with DrizzleProvider and pass in an options object. You can also pass in your existing store. See our documentation for more information on using an existing Redux store.

    <DrizzleProvider options={options}>
      <App />
    </DrizzleProvider>
  4. Wrap your components using the drizzleConnect function. It has the same API as the connect() function in react-redux. See their docs here.

    import { drizzleConnect } from 'drizzle-react'
    
    const mapStateToProps = state => {
      return {
        drizzleStatus: state.drizzleStatus,
        SimpleStorage: state.contracts.SimpleStorage
      }
    }
    
    const HomeContainer = drizzleConnect(Home, mapStateToProps);

    See Drizzle State in the Drizzle docs for the entire state tree.

  5. Get contract data by accessing the contracts via context. Calling the data() function on a contract will first check the store for a cached result. If empty, Drizzle will query the blockchain and cache the response for future use. For more information on how this works, see How Data Stays Fresh in the Drizzle docs.

    Note: We have to check that Drizzle is initialized before fetching data. A one-liner such as below is fine for display a few pieces of data, but a better approach for larger dapps is to use a loading component.

    // For convenience
    constructor(props, context) {
      super(props)
    
      this.contracts = context.drizzle.contracts
    }
    
    // If Drizzle is initialized (and therefore web3, accounts and contracts), fetch data.
    // This will update automatically when the contract state is altered.
    var storedData = this.props.drizzleStatus.initialized ? this.contracts.SimpleStorage.methods.storedData.data() : 'Loading...'

    The contract instance has all of its standard web3 properties and methods. For example, sending a transaction is done as normal:

    this.contracts.SimpleStorage.methods.set(this.state.storageAmount).send()

Recipe: Loading Component

The following wrapper and component will detect when your dapp isn't ready and allow you to display the appropriate status or course of action:

LoadingContainer.js

import Loading from './Loading.js'
import { drizzleConnect } from 'drizzle-react'

// May still need this even with data function to refresh component on updates for this contract.
const mapStateToProps = state => {
  return {
    drizzleStatus: state.drizzleStatus,
    web3: state.web3
  }
}

const LoadingContainer = drizzleConnect(Loading, mapStateToProps);

export default LoadingContainer

Loading.js

import React, { Component, Children } from 'react'

class Loading extends Component {
  constructor(props, context) {
    super(props)
  }

  render() {
    if (this.props.web3.status === 'failed')
    {
      return(
        // Display a web3 warning.
        <main>
          <h1>⚠️</h1>
          <p>This browser has no connection to the Ethereum network. Please use the Chrome/FireFox extension MetaMask, or dedicated Ethereum browsers Mist or Parity.</p>
        </main>
      )
    }

    if (this.props.drizzleStatus.initialized)
    {
      // Load the dapp.
      return Children.only(this.props.children)
    }

    return(
      // Display a loading indicator.
      <main>
        <h1>⚙️</h1>
        <p>Loading dapp...</p>
      </main>
    )
  }
}

export default Loading