Giter VIP home page Giter VIP logo

react-native-simple-state's Introduction

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.

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).

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.

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.

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

react-native-simple-state's People

Contributors

haywood1 avatar lordhaywood avatar

Stargazers

 avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.