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

util-react

v0.0.38

Published

react项目中使用的工具集

Downloads

18

Readme

util-react

react项目中使用的工具集

安装

$ npm install -g util-react

在线DEMO

genInputGetter

Form

使用文档

createApp(component[,htmlElement])

创建一个App类,用于渲染React组件

  • component React组件
  • htmlElement HTMLElement, 需要渲染的DOM节点

返回 App类的对象

Class:App

create()

通过ReactDOM.render渲染组件

getIns()

获取单例,只有第一次执行时会通过ReactDOM.render渲染组件

example

import {createApp} from 'util-react'
import React from 'react'
function Demo () {
    return <div>demo</div>
}

createApp(Demo).getIns() 

genInputGetter(context, key[, valueGetter[, opts]])

类似input组件的值变化时,同步更新React组件内部的state

context

React组件的实例对象

key

指定与input组件绑定的state中的key,如inputValueinput.value

valueGetter(event)

从event中获取input组件的值,默认为 event => event.target.value

opts

onChange(value, event)

input组件值变化时触发,valuevalueGetter中获取的值。此时key指定的变量的值未更新

callback

input组件值变化后,会通过setState更新key中声明的变量。callback为setState的回调,此时key指定的变量的值已更新

example

import {genInputGetter} from 'util-react'
import React, {Component} from 'react'

class Demo extends Component {
    constructor (opts) {
        super(opts)
        this.state = {
            input: {
                value: ''
            }
        }
    }

    render () {
        return (
            <div>
                <input 
                    type="text" 
                    onChange={genInputGetter(this, 'input.value', event => event.target.value, this.onInputChange)} />
                <div>{this.state.input.value}</div>
            </div>
        )
    }

    onInputChange (...args) {
        console.log(args)
    }
}

Class:Form

Form组件会对它包容的元素进行处理,对其中特定的元素进行取值,在内部提交事件发生后,会执行this.props.onChange,对外提供收集到的数据。

收集数据

为元素添加属性nameForm组件会为其添加onChange属性,来收集该元素的值。getter属性声明如何通过onChange收集值(默认值为val => val)。

提交

为元素添加immediate属性,声明该元素每次值的变化都会触发Form组件的提交事件。

为元素添加属性submitForm组件会为其添加onClick属性。onClick触发时会触发Form组件的提交事件。

初始化数据

Form组件添加defaultValue属性来初始化Form组件内部的值 为Form组件添加value属性来绑定Form组件内部的值

demo

<Form defaultValue={{abc: '456'}} onChange={val => console.log(val)}>
    <input immediate name="abc" getter={event => event.target.value}></input>
    <div>
        <input name="password" placeholder="password" getter={event => event.target.value} onChange={this.onChange.bind(this)}></input>
    </div>
    <button submit>search</button>  
</Form>