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-native-simple-state

v1.0.0

Published

React Native module to allow for setting and getting of nested objects within the components state via strings

Downloads

3

Readme

React Native Simple State

npm version Build Status

React Native Components State doesn't provide a clean way to update or retreive nested object elements. React Native Simple State is a way to solve this by adding further methods to state providing editing sub elements without having to requesting the whole state to update a single node deep within the object.

You will need to use a key to access specific nested objects, using full stops to seperate the keys for each objects. Therfore using posts.2.author.name would link to John Smith from the following object. With posts being the first element from the main object, then 2 will take the element from the array with index 2. Finally author.name will retreve the author object from the selected nested post object and retrive the name value.

{
 "posts": [
   {
     "author": {
       "name": "Dave Turner"
     }
   },
   {
     "author": {
       "name": "John Smith"
     }
   }
 ]
}

This library provides an Component super class that can be used in replacement of the React Native Component super class. It is to use as it light weight and only will need an extra import line, and using saves your development time, code space and improves your effeciency.

The following example creates a component that will allow use of the extra fuctionality provided by React Native Simple State:

import React from 'react';
import Component from 'react-native-simple-state';

class SimpleStateComponent extends Component {
 constructor(props) {
   super(props);
 }
};

Install

npm install react-native-simple-state --save

Usage

Getting Started

Importing

Import the Component like this

import Component from 'react-native-simple-state';

Linking to React Native Component

Import the Component like this, just as you would with the React Native Component. Don't forget to parse the props of the parent.

class SimpleStateComponent extends Component {
 constructor(props) {
   super(props);
 }
 ...
}

Methods

placeState

Writes to the nested object referred to by this key. If the object does not exist yet, it will be created. If you activate merge, the provided object can be merged into the existing object.

Params

| name | type | description | | ------- | ------ | --------------------------------------------------------------------------- | | key | string | The key that specifies the nested object to insert the value into the state | | data | object | An object of the fields and values for the nested object. Value must not be null. | | merge | boolean (optional) | An object to configure the placeState behavior. Pass true to only replace the values specified in the data argument. Fields omitted will remain untouched. Value must not be null. |

retreveState

Reads the document referred to by this key.

Params

| name | type | description | | ------- | ------ | --------------------------------------------------------------------------- | | key | string | The key that specifies the nested object retreve the value from the state |

Returns

| type | description | | ------ | ------------------------------------------------------------------------------------- | | object | The value of the nested object |

updateState

Updates fields in the nested object referred to by this key. The update will fail if applied to a document that does not exist.

Params

| name | type | description | | ------- | ------ | --------------------------------------------------------------------------- | | key | string | The key that specifies the nested object the value to be merged with, within the state | | value | object | An object containing all of the fields and values to update. Value may be repeated. |

deleteState

Deletes the nested object referred to by this key

Params

| name | type | description | | ------- | ------ | --------------------------------------------------------------------------- | | key | string | The key that specifies the nested object to remove from the state |

Examples

Getting a Post

This examplar module allows you to get a post by using the key to get a specific.

import React from 'react';
import Component from 'react-native-simple-state';

class CreatePostComponent extends Component {
 constructor(props) {
   super(props);
   this.state = {
     posts: [
       {
         author: {
           name: "Bob Turner"
         },
         body: "Hello"
       }
     ]
   };
 }
 
 getPost(postId) {
   this.retreveState(`posts.${postId}`);
 }
}

Adding Post

This examplar module allows you to create a post by creating a object and adding it directly into a nested object (in this case, and array).

import React from 'react';
import Component from 'react-native-simple-state';

class CreatePostComponent extends Component {
 constructor(props) {
   super(props);
   this.state = {
     posts: []
   };
 }
 
 createPost(postId, authorName, body) {
   this.placeState(`posts.${postId}`, {
     author: {
       name: authorName
     },
     body: body,
     lastUpdated: new Date()
   });
 }
}

Updating a Post

This examplar module allows you to get a post by using the key to get a specific.

import React from 'react';
import Component from 'react-native-simple-state';

class CreatePostComponent extends Component {
 constructor(props) {
   super(props);
   this.state = {
     posts: [
       {
         author: {
           name: "Bob Turner"
         },
         body: "Hello"
       }
     ]
   };
 }
 
 updatePostBody(postId, body) {
   this.updateState(`posts.${postId}`, {
     body: body
   });

   // has same behavior has 

   this.assignState(`posts.${postId}`, {
     body: body
   }, true); // merge activacted
 }
}

Deleting a Post

This examplar module allows you to get a post by using the key to get a specific.

import React from 'react';
import Component from 'react-native-simple-state';

class CreatePostComponent extends Component {
 constructor(props) {
   super(props);
   this.state = {
     posts: [
       {
         author: {
           name: "Bob Turner"
         },
         body: "Hello"
       }
     ]
   };
 }
 
 deletePost(postId) {
   this.deleteState(`posts.${postId}`);
 }
}