Git Product home page Git Product logo

react-native-redux's Introduction

React Native Redux

npm PRsBadge npm npm

Implement react redux into your app in just One Step!

  • No Store configuration needed!
  • No MiddleWares needed, no more dispatch complications.
  • No Reducers or ACTIONS required.
  • No Persistance configuration needed, All data are persisted!
  • Very simple way to change store state, just like Component setState !
  • Simply connect your components with simpler connect function
  • Easily use hooks for your functional component, Dive deep in state up to 5 levels!.
  • Built on redux, react-redux and redux-persist, No previous experience needed.
Note that this lib was known before as Step React Redux.

Instalation

  • Install & Link React Native Async Storage // SKIP this step if already using react-native async storage

  • Then install React Native Redux

    npm i react-native-redux - OR - yarn add react-native-redux

Usage

Provider

Props
initialState: object?
loading: JSX.Element?
Usage
import React from "react"
import { Provider } from "react-native-redux"
import AppContainer from "../navigation" // Path to Root Navigation


const myInitialState = { /* your initial state */ }
/*
 Important Note: state will initialize for first time only,
 then you have to use xSetState or setStateForKey to change it,
 If you want to reinitialize state, you have to call xResetState once.
 See xResetState below
*/

export default ()=> (
  <Provider 
   initialState={myInitialState} 
   loading={/* your loading UI*/}
  >
    <AppContainer />
  </Provider>
)

connect

Arguments
WrappedComponent: Component
requiredKeys: string[]?
Usage
import React from "react"
import { connect } from "react-native-redux"

class UserPage extend React.Component {
  // Your Component goes here
}

// this will connect all your store to UserPage component props
export default connect(UserPage) 

/*
 OR you can choose what keys this component using by providing requiredKeys argument
 NOTE: Make sure that your requiredKeys values are already initiated.
*/ 

export default connect(UserPage, ["user", "someKey", "anotherKey"])


// You can also connect to deep state ( Up to 5 levels ) using dotted key. 
export default connect(UserPage, ["user.name"])
// a prop with key "user_name" will be connected

// You can change deepKeyReplacer as a third optional argument
export default connect(UserPage, ["user.name"], "-")
// a prop with key "user-name" will be connected


xSetState

Arguments
state: object
Usage
import { xSetState } from "react-native-redux"

// Anywhere in your code

xSetState({ user: { id: 1, name: "Some Name" } })
// console logs => ReactNativeRedux.user, { id: 1, name: "Some Name" }
// Now all your connected components will have "user" prop


// Usage with API

async getMyData(){

    xSetState({ isFetching: true })

    try {

      const response = await fetch("http://www.myServer.com/api/myData")
      const responseJson = await response.json()
      xSetState({ isFetching: false, myData: responseJson  })

    } catch (error) {

      alert(error.message)

      // Remeber that You can use xSetState ANYWHERE! as much as you want !
      xSetState({ isFetching: false })
    }

}

setStateForKey

Arguments
key: string
state: object
Usage
import { setStateForKey } from "react-native-redux"

// Similar to xSetState
// plus it can be used to set deep state up to 3 levels

setStateForKey("user", { id: 1, name: "Some Name" })

// console logs => ReactNativeRedux.user, { id: 1, name: "Some Name" }
// Now all your connected components will have "user" prop

// Usage to set deep state

setStateForKey("user.name", "New Name" )

// console logs => ReactNativeRedux.user.name, "New Name"
// Remeber that You can use setStateForKey ANYWHERE!

getStateForKey

Arguments
key: string
Usage
import { getStateForKey } from "react-native-redux"

// Similar to setStateForKey
// but it can be used to get state and deep state

const userData = getStateForKey("user") 
console.log(userData) // => { id: 1, name: "Some Name" }

const userName = getStateForKey("user.name") 
console.log(userName) // => "Some Name"

// getting state for unknown key will return null
const someValue = getStateForKey("someKey") 
console.log(someValue) // => null

const anotherValue = getStateForKey("anotherKey.subKey") 
console.log(anotherValue) // => null


// You may add fallback value as second optional argument
/// default fallback is null
const undefinedValue = getStateForKey("somekey.subKey.subsubkey","Hello World") 
console.log(anotherValue) // =>  Hello World

// Remeber that You can use getStateForKey ANYWHERE!

useStateX (Hook)

You may like to try our new set of hooks React Stateful Function.
Usage
import React from "react"
import { useStateX } from "react-native-redux"

// Hooks Are used inside functional components

const MyComponent = (props) => {

	// Depth: 2 levels
	const isLoggedIn = useStateX("user.loggedIn")
	
	// Depth: 3 levels
	const userName = isLoggedIn ? useStateX("user.data.name") : "Guest"
	
  // NOTE THAT DEPTH TREE SHOULD BE INITIALIZED BEFORE HOOKING IT
  
  // You may add fallback value as second optional argument
  // default fallback is null
  const undefinedValue = getStateForKey("somekey.subKey.subsubkey","Hello World") 
  console.log(anotherValue) // =>  Hello World
	
	return (
		<>
		// Your Component goes here
		<Text>Name: {userName}</Text>
		</>
	)
	
  
}

export default MyComponent

xResetState (Dev Only)

Usage
import React from "react"
import { xResetState } from "react-native-redux"

/*
Top level index in your code, call this method once during your develeopment process
to allow you to reinitialize your state again from Provider initialState prop
*/

// Call this Once, Then Don't forget to remove it.

xResetState()

react-native-redux's People

Contributors

dependabot[bot] avatar joeherbst avatar yamingue avatar yasser-g avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

react-native-redux's Issues

setStateForKey : TypeError: key.includes is not a function. (In 'key.includes('/')', 'key.includes' is undefined

Hello,
When using setStateForKey I get an error: https://gyazo.com/b6b0db8b6222b1b504190b355f6c9d54

However xSetState works perfectly. Perhaps I am using the setStateForKey function incorrectly?

`class AuthenticationLoginScreen extends React.Component {

constructor(props) {
    super(props);
    
}


handleLogin = () => {
    setStateForKey("user", { id: 1, name: "Some Name" })
};

render() {

    return (
        <View style={styles.container}>
            <SafeAreaView><Text>{this.props.user.name}</Text></SafeAreaView>
            <TouchableOpacity onPress={() => this.handleLogin()}>
                <Text>test</Text>
            </TouchableOpacity>
        </View>
    )
}

}

export default connect(withNavigation(AuthenticationLoginScreen));`

Thank

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.