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

@hyder/component

v1.5.2

Published

用于创建hyder组件

Downloads

39

Readme

@hyder/component

提供一致的方式创建React模块和应用,让react应用开发更轻松。

基于@hyder/component的模块能够运行在hyder环境中, 其中的逻辑代码运行在js-core环境中, 也能够运行在普通的react应用中, 提升模块重用度,简化复杂模块的开发难度,特别适合团队开发。

开始使用

  1. 添加依赖
yarn add @hyder/component
  1. 创建组件

组件(如需要较复杂的逻辑)可由ViewModel组成, 在hyder中model 会运行在js-core环境中。

模块的开发方式和目录结构并无要求,但推荐如下:

- Counter/   # 模块
  - index.js   # 入口,如复杂的话可衍生出多个子组件
  - model.js   # 逻辑代码,框架本身不依赖于这个名称,逻辑复杂可加载其他文件。
  - style.scss # 样式

hyder babel插件会根据model中的注解加载model到js-core中(这个特性还在内部开发中)

模块目录和代码可参考Counter示例

借鉴于dva的最佳实践,model由state, reducers, 和effects组成。

model.js

export default {
  // state: { ... }
  state: props => ({ count: 0 }),  // 模块的state可由props初始化

  reducers: {
    up(state, action) {
      return nextState;
    },

    ...
  },

  effects: {
    * mount(action, { put, select }) {
      ...
      yield put({ type: 'load', payload: ... });
    }
  }
};

index.js

通过提供的withModel高阶组件来组合上面定义的model。

import { withModel } from '@hyder/component';   // 引入withModel
import model from './model.js';


const View = ({ count, dispatch }) => (
  <div>
    <div className="count">{count}</div>
    <div className="buttons">
      <button className="button" onClick={() => dispatch({ type: 'up', step: 3 })}>Up</button>
      <button className="button" onClick={() => dispatch({ type: 'down', step: 4 })}>Down</button>
    </div>
  </div>
);


export default withModel(model)(Counter);

对于[email protected]以上版本,可使用React Hooks集成model。

import { useModel } from '@hyder/component';
import model from './model';

const Counter = props => {
  const [{ count }, dispatch] = useModel(model);
  return (
    <div>
      <div className="count">{count}</div>
      <div className="buttons">
        <button className="button" onClick={() => dispatch({ type: 'up', step: 3 })}>Up</button>
        <button className="button" onClick={() => dispatch({ type: 'down', step: 4 })}>Down</button>
      </div>
    </div>
  );
};

接入到redux

配合react-redux可在应用级别使用, 无需依赖其他库,可方便集成到现有redux应用中。

接入

import { compose } from 'redux';
import { createStoreEnhancer } from '@hyder/component';   // 载入storeEnhancer

import pageModel from './models/page';  // 载入应用的模块,可以有多个


const hyderEnhancer = createStoreEnhancer();

const store = createStore(
  reducer,
  {},
  compose(
    applyMiddleware(promiseMiddleware),
    hyderEnhancer
  )
);

// 添加应用的模块
hyderEnhancer.add([
  pageModel
]);

使用

  1. model

model的编写和上述一致,多了name,和少了mount对生命周期的支持,因为应用级别的model不和某个具体的react组件绑定。

详情可见示例

// models/page.js

export default {
  name: 'page',    // 名称,在dispatch中作为名字空间

  state: {
    count: 0       // 初始state, 由于应用级model不和具体react组件关键,所以只是个普通对象
  },

  reducers: {
    up(state, action) {
      return nextState;
    }
  },

  effects: {
    * down(action, { put, select }){
      ...
    }
  }
};
  1. view

像正常的react-redux的方式使用就可以啦。

const enhance = connect(v => v);
const View = enhance(({ page, dispatch }) => (
  <div className="page-view">
    <h1>{page.title}</h1>
    <div>
      <div className="count">{page.count || 0}</div>
      <div className="buttons">
        <button className="button" onClick={() => dispatch({ type: 'page/up', step: 3 })}>Up</button>
        <button className="button" onClick={() => dispatch({ type: 'page/down', step: 4 })}>Down</button>
      </div>
    </div>
  </div>
));