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

recomponent

v0.1.1

Published

Writing react components redefined

Downloads

5

Readme

recomponent

build status bitHound npm downloads npm version

Writing react components redefined.

recomponent separates component view and logic while preserving its context. It creates a new way of structuring the components and allows us to keep our component clean and layered.

Installation

npm install --save recomponent

Usage

Create an index.js file under your component folder. Let's try to separate the concerns. Import your logic, view and css into this file and wrap them using component.

Example

Check test folder for additional examples.

import component from 'recomponent';
import MyComponentLogic from './MyComponent.js';
import MyComponentTemplate from './MyComponent.jsx';
import './MyComponent.css';

export default component({
  logic: MyComponentLogic,
  template: MyComponentTemplate,
  styles: {}
});

Component

Wraps your logic, view, store and styles in a single whole component.

component({ ... })

/**
 * @params {object} - { logic, template, store, styles }
 *
 * @paramsKeys logic {component} optional - class-based component (wc extends `component`)  
 * @paramsKeys template {function} required - react view that can be rendered.
 * @paramsKeys store {object} optional - initial store
 * @paramsKeys styles {object} optional - styles to use on view
 */
 component({ logic, template, store, styles })

Store

recomponent provides optional store from your component. This store is just like a state of your component. The reason why it's separated aside from using component's state itself, because you might likely to have your own stored data provider that has its own purpose.

Use this as custom store for your specific component. If child components are also wrapped with recomponent, they can directly access the store from their context (this.store or context.store for dumb components).

You can add store by adding store key in declaration:

export default component({
  ...
  store: { title: 'My Store' }
})

this.store

Stored data from the component it was defined.

this.setStore({ key: value })

Update store and forces the component to update.

Structure

By using the library, we will end up restructuring our component in different way. If our component is class-based, the logic and view (render) is separated. Thus, it's even more clean than before.

├── MyComponent          # Your component.
│   ├── index.js         # Main entry point. You can do `recomponent` init here.
│   ├── MyComponent.js   # Your logic. You can also do `recomponent` here.
│   ├── MyComponent.jsx  # Your template. File ext. is optional (change the filename if using `.js`)
│   └── MyComponent.css  # Your css

Rules

  1. If your component is class-based, you can remove your render method in your logic. Also, there is no point on using recomponent on dumb components. But if you really like to, you can still declare it without the logic.
  2. Heard about arrow functions are already bound to its scope? but we want our react templates to still access it's own context (this) like we normally do. Yes, I want you to use normal function
// MyComponent.jsx
export default function (props) { // please do
  // this - you'll get everything from the logic
  // props - ah normal props, but you can get it also from the context.
  return (<div></div>)
};

Maintainer

John Edward Escuyos

License

MIT