1.3.3 • Published 1 year ago

react-global-state-management v1.3.3

Weekly downloads
-
License
MIT
Repository
github
Last release
1 year ago

react-global-state-management

When you look about global state management system, you will find DataContext or even more complex like Redux The problem with those two are

1- DataContext is a global context but for it to be able to work with state you will have to assign it a mutated data and that will cause it to update globaly, which is in many times not nessecry needed, and you cannot controll how it works.

2- Redux is just to complex to setup and I would not like working with ever.

So I build I library where you can reach your data globally and also controll to which components and its properties should trigger a rebuild when changing the data.

This library works for all apps that uses react as its base, eg like react-native and reactjs

Install

npm i react-global-state-management

Example

Have a look at Snack

Usage

import GlobalState from 'react-global-state-management';
const data = GlobalState({
  text: 'someText',
  item: { counter: 0, item2: { counter: 0 } },
  arr:[{test: 0}]
});

const TTX = () => {
  // bind mutated to this component 
  // You could also have a global mutation with only "data.hook();"
  // below mean that only when data.item.item2.counter changed, it will trigger rebuild for this component.
  data.hook("item.item2.counter"); 
  return <Text>counterComponentWithHook: {data.item.item2.counter}</Text>;
};

const TTX2 = () => {
  data.subscribe(
    (item, props) => {
      console.log('data.item.counter Changed in TTX2 component', props);
    },
    "item.counter"
  );
  return <Text>counterComponentWithoutHook: {data.item.counter}</Text>;
};


export default function App() {
  data.subscribe((item, props) => {
    console.log('data Changed in App component', props);
  });
  return (
    <View>
      <Text>
        AppComponentCounter: {data.item.counter}
      </Text>
      <TTX />
      <TTX2 />
      <Button
        title="inc Counter"
        onPress={() => {
          data.item.counter++;
        }}
      />

      <Button
        title="inc counter2"
        onPress={() => {
          data.item.item2.counter++;
        }}
      />

        <Button
        title="inc array"
        onPress={() => {
          data.arr[0].test++;
          data.arr.push({test:5}) // this will also trigger change
        }}
      />
    </View>
  );
}

Properties and events

Rebuild the component when a change happened

hook() OR hook("item.counter", ...)

Rebuild the component with conditions

on<boolean>("item.enabled", (value)=> value === true)

Subscribe to changes when the global data change. This works like useEffect except You will have more control over your updates and this will not trigger rebuild

// global
data.subscribe(
    (item, props) => {
      console.log('GlobalState Changed');
    }
  ); 
  
  //OR
  
 // Only when item.counter
  data.subscribe(
    (item, props) => {
      console.log('item.item2.counter Changed in TTX2 component');
    },
    "item.counter",...
  );  
  
NameDescriptions
hookevent that bind properties to the components so that when changes happend a rebuild happend in component
subscribeevent work as useeffect without rerendering the component
oncreate a hook that trigger rebuild on conditions that you specify
execludeComponentsFromMutationsargs that is passed to the global data that execlude some properties from herarkie binding(the library create set and get for each property. And this is done herarkie. You can execlude objects from this. eg objects and not (number, boolean,string) propeties)
disableTimerThe library create a settimeout for each changes so that when two changes happend at the same time only one call will be triggered. You can disable this and trigger a call after each change directly, this is usefull when using it in games.
onChangeYou can use this prop outside components, as subscribe and hooks can only be used in components
stringifyThis solve the self refrences issue when parsing the object to json string
triggerChangeSometimes you are using props in components that do not have hooks or subscribe, using this method you could trigger updates to those components by specifing thair Identifiers

Exelude objects in array

Typescript will complain when you add a path that is contained in array, as execludeComponentsFromMutations only take object path in its array. To solve this issue, you are able to assign a method to execludeComponentsFromMutations that return true if it execluded and false if not

  const data = GlobalState({
  text: 'someText',
  item: { counter: 0, item2: { counter: 0 } },
  arr:[{test: 0, item: {text:""}}]
}, (path)=> {
  if (path === "arr.item")
      return true; // execlude `arr.item` from mutation
    return false;
});
1.2.8

1 year ago

1.2.7

1 year ago

1.2.6

1 year ago

1.2.5

1 year ago

1.3.3

1 year ago

1.3.2

1 year ago

1.3.1

1 year ago

1.3.0

1 year ago

1.2.9

1 year ago

1.2.4

1 year ago

1.2.3

1 year ago

1.2.2

1 year ago

1.2.1

1 year ago

1.2.0

1 year ago

1.1.9

1 year ago

1.1.8

1 year ago

1.1.7

1 year ago

1.1.6

1 year ago

1.1.5

1 year ago

1.1.4

1 year ago

1.1.3

1 year ago

1.1.2

1 year ago

1.1.1

1 year ago

1.1.0

1 year ago

1.0.9

1 year ago

1.0.8

1 year ago

1.0.7

1 year ago

1.0.6

1 year ago

1.0.5

1 year ago

1.0.4

1 year ago

1.0.3

1 year ago

1.0.2

1 year ago

1.0.1

1 year ago

1.0.0

1 year ago