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

@mavega/react

v1.2.2

Published

Create React Class, in a way that synchronizes with oloo. Also allows the use of Pure React classes.

Downloads

38

Readme

LGD

  • Pulled create-react-class out of react.
  • Extended functionality to work for React.PureComponents and React.Component.

Why to use this module.

create-react-class requires you to change the way you use React. Forget about getInitialState or getDefaultProps. Just use state like you normally would and when the react class gets created well initialize the state for you.

const Obj = {
  create(props) {
    this.state = {
      header: null
    }
  },
  componentDidMount() {},
  componentDidUpdate() {},

  render() {

  }
};

We also handle static functions like getDerivedStateFromProps for you so just define it like you would any function. no need to place it inside a statics variable.

Obj.getDerivedStateFromProps = (props, state) => {

}

Other Modules

  • @mavega/oloo
    • Helps with inheritance of Objects.
  • @babel/plugin-transform-react-display-name
    • It can help with errors that occur. The name of the Object will show in Stack Trace.

Vscode Extensions

  • JavaScript to Typescript Converter & Syntax Highlighter
    • Help with AutoComplete in vscode for React methods.
    • Helps with inheritance when using plain Objects.
    • Provides Linting of Objects for CreateReactClass.
  • JS/React Snippet Extension.

Speed

For Comparison a typically Class takes less than 100 milliseconds to create 1 million times.

Guide

import BaseModal from 'SRC/Modals/BaseModal';
import propTypes from 'prop-types';
import styles from 'SRC/Utilities/Styles';

const { Oloo } = require('@mavega/oloo');
const { createReactPure } = require('@mavega/react');

/**
* @description A modal that helps with handling input from a user.
* the extends doc helps with autocomplete for vscode.
* @type {InputModalType}
* @extends {BaseModalType, React.Component<InputModalProps, InputModalState>}
*/
const InputModal = {
  // Helps with debugging if error occurs. Should be removed in production by babel or something.
  // displayName also works and is ideal.
  constructor: function InputModal() {},
  displayName: 'InputModal',

  staticVariable: 'defined statics here',
  staticNumModals: 0,

  /**
  * @description Initialize an instance of InputModal.
  * This method can return nothing and we will create the react instance using
  * the object passed into the createReactComponent method.
  * @param {InputModalProps} props the props passed by react.
  * InputModalProps gets generated by Extension i mentioned above.
  * @returns {InputModalType}
  */
  create(props) {
    // This sets up a chain of prototypes that will be assigned to a new react instance.
    // Extremely Basic jist is Object.setPrototypeOf(inputModal, baseModal);
    // input.__proto__ === InputModal
    // input.__proto__.__proto__ = BaseModal
    // We will keep this chain on the react class instance so base methods can be used later on.
    const inputModal = Oloo.assign(BaseModal.create(), InputModal);

    // This will get overwritten by the assignment(inputModal.modalStyle) below. We take all the variables added to the inputModal and assign them to the react instance.
    // If you only want to use 'this' then make sure you call create of child like so.
    // BaseModal.create.call(this, arg0, arg1)
    this.modalStyle = {};
    this.index = null;

    inputModal.modalStyle = {
      ...inputModal.modalStyle,
      ...styles.messageBoxStyle
    };

    /**
     * @description This is how you set state of the Object.
     * State can't be used in PureComponent.
     */
    input.state = {
      /**
       * Calling props like this will work even it its not in default props.
       */
      showing: props.showing
      showing: this.props.showing // either works.

      /**
       * You can't access an obj on props if it is not defined in defaultProps.
       */
      fails: props.obj.fails

      /**
       * We defined this in the default props below.
       */
      succeeds: props.existingObj.succeeds

      example: 'Not available in Pure'
    }

    return inputModal;
  },

  componentDidMount() {
    // Use like normal.
    this.index = InputModal.staticNumModals;
    InputModal.staticNumModals++;
  },

  get value() {
    // Works like normal.
  }

  set value() {
    // Works like normal.
  }

  setStateExample() {
    // Works like normal.
    this.setState({ example: 'i changed state' });

    // Statics will work across all instances in React.
    InputModal.staticVariable = 'access statics like this';
  }

  renderBody() {
    return (
      <div>
        <h1>Hi</h1>
      </div>
    );
  },

  render() {
    if(!this.props.showing) {
      return null;
    }

    // this renderModal is a method on BaseModal.
    return this.renderModal(
      this.renderBody(),
      this.modalStyle,
      this.props.onClosed
    );
  }
};

InputModal.getDerivedStateFromProps = (props, state) => {...};

// This is not necessary since we handle
// getDerivedStateFromProps and getDerivedStateFromError.
// Use it like above.
InputModal.statics = { getDerivedStateFromProps(props, state) {...} }

InputModal.defaultProps = {
  existingObj: {
    // Doesn't need to exist for access in create to work.
    // as long as existingObj is assigned as an obj.
    succeeds: true
  }
}

InputModal.propTypes = {
  /**
  * @description Boolean that determines whether we are showing this modal.
  */
  showing: propTypes.bool.isRequired,

  /**
  * @description Function called when we want to close this modal.
  */
  onClosed: propTypes.func.isRequired
};

export const InputModalObject = InputModal;
export default createReactPure(InputModal, optionalOptions);
import InputModal from 'SRC/InputModal';

// Now you can use it like any React Component.
<InputModal />

Release Notes

1.2.0+

  • Slight Speed improvements.
  • Fixed bug that caused certain methods to not maintain inheritance properly.
    • Deeply nested methods are getting maintain properly.
  • Improved Debugging.
  • Fix bug in development environment where we tried to set proptypes from null obj.

1.1.0+

  • added statics support.
  • No need to have create method anymore.
  • Added fallback to base class propTypes if none are defined.
  • render could only be on base class now.

1.0.0+

  • As close to React as possible.
  • One Singular Constructor called create().
  • Use 'this' in create and receive props.
  • Assign defaultProps, propTypes to static Object.