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

declarative-react-component

v0.1.5

Published

A helper for creating React components using a simpler, declarative syntax.

Downloads

1

Readme

Declarative React Component

A helper for creating React components using a simpler, declarative syntax. The module exposes a function DeclarativeReactComponent. DeclarativeReactComponent accepts a config object and returns a reusable component class.

Usage

Using DeclarativeReactComponent, we will still create components in .jsx files. Consider the following example, which is a complete .jsx file for creating a component called ExampleComponent.

// example-component.jsx
import React, { Component } from "react";
import {DeclarativeReactComponent} from "declarative-react-component"

export default DeclarativeReactComponent({
  "props" : [
    "someProp"
  ],
  "data" : {
    someNumber : 0,
    someString : "room"
  },
  "methods" : {
    up : function() {
      this.someNumber++;
    },
    down : function() {
      this.someNumber--;
    },
    handleFormChange : function(newValue) {
      this.someString = newValue;
    }
  },
  "template" : function() {
    return (
      <div>
        <div>
          <h2>I'm holding the state value {this.someNumber} and the prop {this.someProp}.</h2>
          <button
            type = "button"
            onClick = {this.up}
          >Add 1</button>
          <button
            type = "button"
            onClick = {this.down}
          >Subtract 1</button>
        </div>
        <div>
          <h2>I'm also holding the string: {this.someString}</h2>
          <p>
            <input type = "text" value = {this.someString} onChange={(e)=>this.handleFormChange(e.target.value)}></input>
          </p>
        </div>
      </div>
    );
  }
});

The result is a React component we can use in the ordinary way, such as this.

import React, { Component } from "react";
import ExampleComponent from "./example-component.jsx"

class DemoApp extends Component {
  render() {
    return (
      <div>
        <div>
          <ExampleComponent someProp = "propValue"/>
        </div>
      </div>
    );
  }
}
export default DemoApp;

DeclarativeReactComponent and configObject

The function DeclarativeReactComponent requires just one argument: the configuration object, which henceforth we call configObject.

props

The names of props must be declared in configObject.props as an array of strings. If a prop is not declared here, it will not be available on this;

data

The keys and values declared in configObject.data get transformed into properties on the React component's state object, but this is something you do not need to worry about. It works automagically. Any change made to elements inside data will pass through and cause re-rendering as they usually do in React.

methods

Each method declared inside configObject.methods gets attached directly to this. Methods can mutate values from configObject.data but they may not mutate values from configObject.props.

Actually, methods do not have direct access to the entire object. For example, it is not possible for one method to overwrite another method. Methods have access to read/write data and to read-only props, and they can access anything explicitly passed in as an argument.

template

The value of configObject.template should be a function much like the usual render function in a React component class. The primary difference is that in our template function this refers to a limited object that will only allow us to alter elements declared or defined in the configObject.

License

This project and its source code are released under the MIT license. For details, see LICENSE.