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

react-context-tools

v1.0.13

Published

react-context-tools React component

Downloads

4

Readme

版本1.0.13

1. actions如果实现了componentDidMount方法,则在Provider调用componentDidMount时调用此实现

2. 同理,如果实现了componentWillUnmount方法,则在Provider调用componentWillUnmount时调用此实现

3. 调用setState之后,如果渲染时间超过了500毫秒,在process.env.NODE_ENV === "development"状态下,控制台会打印log发出警告

actions: {
        componentDidMount: function(){
            console.log("did mount")//在Provider渲染结束调用componentDidMount时,会在控制台打印出did mount
        },
        ....
}

版本1.0.12 新增delegate(Component, ...wrappers)函数

//可以合并多个Consumer,将合并之后的props传给Component,后面的props.customeKey会覆盖前面的props.customKey
const DemoConsumer = delegate(Demo, Demo1Consumer((state)=>{
    const {query, data} = state;
    return {
        query,
        data,
        customKey: 2//会被覆盖
    }
}), Demo2Consumer((state)=>{
    const {query2} = state;
    return {
        query2,
        customKey: 3//不会被覆盖
    }
}, (actions)=>{
    return {}
}));

版本1.0.11 新增getStateNow()函数

export const {Consumer, getStateNow} = initStore({
    initialState: {a: 1},
    actions: {}
})

console.log(getStateNow());//print {a: 1}

版本1.0.4新增getState()函数

initStore({
    initialState: {},
    actions: {
       doSomething: function(){
           this.getState()//利用Promise实现
                 .then(state=>{//得到最新的state
           
                 })     
       }
    }
})

版本1.0.3接口变动

initStore(..)返回的Consumer使用方法有所变动,由原先的Consumer(Component)变为Consumer()(Comonent), 使用方法如下,你可以根据你的组件所需的props进行映射

//示例一
const DemoConsumer = Consumer()(Demo);
//示例二
const mapStateToProps = (state)=>{
    const {query} = state;
    return query
};
const DemoConsumer = Consumer(mapStateToProps)(Demo);
//示例三
const mapStateToProps = (state)=>{
    const {query} = state;
    return query
};
const mapActionsToProps = (actions)=>{
    return actions;
};
const DemoConsumer = Consumer(mapStateToProps, mapActionsToProps)(Demo);

Guide

React版本需要>=16.4,基于React Context API封装的工具

npm install --save react-context-tools

export const {Consumer} = initStore({
    initialState: {
        query: {}
    },//此处初始化state,会成为Provider的state
    actions: {
        yourFunctionName1: function(){},//只能使用此种方式定义,不能使用箭头函数,因为需要调用bind(this)方法
        yourFunctionName2: function(param){}
    }//在此处定义function
})
//actions下自定义的函数中,this被bind到Provider中了,可以使用的函数如下
this.setState((state)=>{//React原生的方法
    return yourNewState;
})

this.setState(yourNewState);//React原生的方法

this.getState()//利用Promise实现
.then(state=>{//得到最新的state

})

Example

1.配置你的store

import {initStore} from 'react-context-tools';

export const {Consumer} = initStore({
    initialState: {
        query: {
            start: "2018.06.30",
            end: "2018.07.06"
        },
        data: {}
    },
    actions: {
        getData: function(){//不能使用箭头函数,必须使用function(){/*your code*/}
            return this.getState()//得到最新的state,因为state的更新机制类似一个队列,并不是立即执行的
                .then(state=>{
                    console.log(state);
                    return fetch("/")
                        .then(response=>response.text())
                        .then(text=>{
                            this.setState({
                                data: {}
                            });
                            return text;
                        })
                });
        },
        alterQuery: function (start, end) {
            const query = {
                start,
                end
            }
            this.setState({
                query
            })
        }
    }
})

2.使用store

import {Providers} from 'react-context-tools';

class Demo extends Component {
  componentDidMount(){
      const {getData} = this.props;
      getData()
          .then(json=>{
              console.log(json)
          })
  }
  render() {
    const {start, end, alterQuery} = this.props;

    return <div>
      <h1>start: {start}, end: {end}</h1>
      <button onClick={()=>{
          alterQuery("2018.05.30", "2018.06.06")
      }}>click me to change text</button>
    </div>
  }
}
const mapStateToProps = (state)=>{
    const {query} = state;
    return query
};
// const mapActionsToProps = (actions)=>{
//     return actions;
// };
// const DemoConsumer = Consumer(mapStateToProps, mapActionsToProps)(Demo);
//const DemoConsumer = Consumer()(Demo);
const DemoConsumer = Consumer(mapStateToProps)(Demo);
render(
    <Providers>
        <DemoConsumer/>
    </Providers>,
    document.querySelector('#demo')
);