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

react-cloud-code-mixin

v1.1.2

Published

A Parse Cloud Code mixin for React.js, inspired by the ParseReact Mixin.

Downloads

32

Readme

react-cloud-code-mixin

npm version

Mixin for React components to easily listen for changes and re-render from Parse Cloud Code function calls. The format here is very heavily inspired from ParseReact's Mixin.

How to Use

The first step is to include the mixin in your component like so:

var MyComponent = React.createClass({
  mixins: [ ParseCCMixin ],
  /*...*/
});

This mixin requires you to define a loadData function (analagous to ParseReact's observe function). It returns an object where the keys are the name of the data key where the results will be stored, and the value is an object containing data for running the Cloud Code function (details below). For example:

var MyComponent = React.createClass({
  mixins: [ ParseCCMixin ],
  loadData: function(props, state) {
    return {
      MyCloudCodeData: {
        name: "getMyCloudCodeData",
        params: {
          myFirstParam: "foo",
          mySecondParam: "bar"
        }
      }
    };
  },
  /*...*/
  render: function() {
    //example render function
    return (
      <h1>{this.data.MyCloudCodeData.join(", ")}</h1>
    );
  }
});

Whenever the component mounts, it will issue a function call to the /getMyCloudCodeData endpoint and the results will be stored in this.data.MyCloudCodeData. Any time props or state change, the function will be called again (unless otherwise configured—details below).

The objects associated with the data keys can take the following parameters:

  • name: the name of the Parse Cloud Code function you wish to run.
  • params: the parameters passed to the cloud code function.
  • propDeps: an array with the names of each prop whose value change should cause a reload (e.g. the component will only pull data and re-render if one of these changed).*
  • stateDeps: an array with the names of each state whose value change should cause a reload (e.g. the component will only pull data and re-render if one of these changed).*
  • defaultValue: the default value assigned to the data key on mount.

Example:

var MyComponent = React.createClass({
  mixins: [ ParseCCMixin ],
  loadData: function(props, state) {
    return {
      Users: {
        name: "getUsers",
        params: {
          age: this.props.userAge
        },
        propDeps: ['userAge'],
        defaultValue: []
      }
    };
  },
  /*...*/
  render: function() {
    //example render function
    return (
      <h1>{this.data.Users.join(", ")}</h1>
    );
  }
});

In the example, the getUsers Cloud Code function is called with the parameter age set to this.props.userAge, and data is stored in this.data.Users (whose initial value was []). Our propDeps specify that any time that this.props.userAge changes, the data is pulled again from the server and the component is reloaded.

The propDeps and stateDeps declarations are the biggest departure from the ParseReact mixin; ParseReact reloads in response to any prop or state change. To get this behavior, simply omit the propDeps and statedDeps keys. This will force a reload with any prop or state change. If you don't want to ever reload data in response to props or state, this can also be specified by defining propDeps and stateDeps to be empty arrays.

*Array and Object values not yet supported for propDeps and stateDeps

Additional Component Methods

###this.pendingQueries() Returns an array containing the names of calls that are currently waiting for results. ###this.queryErrors() Returns a map of call names to the error they encountered on the last request, if there was one. ###this.reloadData([callList]) Forces a refresh of Cloud Code calls with names in the callList, if provided, else refreshes data from all the calls. Note: the names in the callList should be the name of the data key in which the data is stored (e.g. the key part of the key-value pairs returned in loadData.

Install

npm install react-cloud-code-mixin

Then, in your React project:

var ParseCCMixin = require('react-cloud-code-mixin');

Or, if you're using ES6/ECMA2015 syntax:

import ParseCCMixin from 'react-cloud-code-mixin'

If you prefer to use a CDN, add the following to your main HTML file:

<script src="https://npmcdn.com/react-cloud-code-mixin/dist/index.min.js" />

Depencies

None! Well, except Parse. But that's a given.

F.A.Q.

Does this replace ParseReact Mixin?

It can, but it doesn't have to. This project started when my company switched from doing Parse queries client-side to making API calls in order to reduce client-side logic and network usage. We use this in our app now instead of ParseReact, but the two can operate side by side (as long as you don't name two data params the same).

Is this production ready?

Like with most open-source software, I can't make any guarantees, but I can tell you it's stable enough that the company I work at is using actively it in production.

How can I contribute?

Feel free to fork this project and create a pull request! I'll merge in anything useful. Thanks!

License

MIT