Skip to content
This repository has been archived by the owner on Feb 17, 2020. It is now read-only.

hosseinmd/react-rest-state

Repository files navigation

react-rest-state

alt text

NPM

install size dependencies

react-rest-state

easy state managment with high performance

install

npm i react-rest-state --save

use

import { init, state, setState, forceUpdate } from "react-rest-state";
...
init({
  name: "alli"
});
...
console.log(state.name)

setState

setState({ name: "mikel" });

or

setState(prevState => {
  return { name: prevState.name + " mikel" };
});

callback

setState({ name: "mikel" }, () => {
  console.log(state.name);
});

connect

import { connect, state, setState } from "react-rest-state";

class app extends React.Component {
  disconnect = connect(
    this,
    ["state1"]
  );
  render() {
    return (
      <View>
        <Text>{state.name}</Text>
        <Button
          title={"change"}
          onPress={() => {
            setState({ name: "jone" });
          }}
        />
      </View>
    );
  }
  componentWillUnmount() {
    this.disconnect();
  }
}

callback

connect(
  this,
  ["name"],
  callbackFunc
);

connect function to states run function when states did update

connect(() => {
  console.log(state.name);
}, ["name"]);

forceUpdate

forceUpdate(["mikel"], callback);

useConnect for Hooks

import { useConnect } from "react-rest-state";

function myComponent(props) {
  //if first argumant is a function and runinng when state did update else component will be update
  useConnect(null, ["name"]);
  return <Text>{state.name}</Text>;
}