Skip to content

Commit b23099d

Browse files
author
Hamed Taheri
committed
init
1 parent f8e90a6 commit b23099d

File tree

4 files changed

+195
-0
lines changed

4 files changed

+195
-0
lines changed

.gitignore

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
# OSX
2+
#
3+
.DS_Store
4+
5+
# Xcode
6+
#
7+
build/
8+
*.pbxuser
9+
!default.pbxuser
10+
*.mode1v3
11+
!default.mode1v3
12+
*.mode2v3
13+
!default.mode2v3
14+
*.perspectivev3
15+
!default.perspectivev3
16+
xcuserdata
17+
*.xccheckout
18+
*.moved-aside
19+
DerivedData
20+
*.hmap
21+
*.ipa
22+
*.xcuserstate
23+
project.xcworkspace
24+
25+
# Android/IntelliJ
26+
#
27+
build/
28+
.idea
29+
.gradle
30+
local.properties
31+
*.iml
32+
33+
# node.js
34+
#
35+
node_modules/
36+
npm-debug.log
37+
yarn-error.log
38+
39+
# BUCK
40+
buck-out/
41+
\.buckd/
42+
*.keystore
43+
44+
# fastlane
45+
#
46+
# It is recommended to not store the screenshots in the git repo. Instead, use fastlane to re-generate the
47+
# screenshots whenever they are needed.
48+
# For more information about the recommended setup visit:
49+
# https://docs.fastlane.tools/best-practices/source-control/
50+
51+
*/fastlane/report.xml
52+
*/fastlane/Preview.html
53+
*/fastlane/screenshots
54+
55+
# Bundle artifact
56+
*.jsbundle

.npmignore

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# Logs
2+
*.log
3+
npm-debug.log
4+
5+
# Dependency directory
6+
node_modules
7+
8+
# Runtime data
9+
tmp
10+
11+
# Examples (If applicable to your project)
12+
examples
13+
example
14+
15+
# source directory
16+
src

index.js

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
import React, { createContext, useContext, useReducer } from "react";
2+
import PropTypes from "prop-types";
3+
4+
export const StateContext = createContext();
5+
6+
/**
7+
* Wrap this component around main App content
8+
* @example
9+
<StateProvider initialState={initialState} reducer={reducer}> App </StateProvider>
10+
*/
11+
export const StateProvider = ({ reducer, initialState, children }) => (
12+
<StateContext.Provider value={useReducer(reducer, initialState)}>
13+
{children}
14+
</StateContext.Provider>
15+
);
16+
17+
/**
18+
* use this Hook inside your any component to access your store
19+
* @example
20+
const [{ user }, dispatch] = useStateValue();
21+
*/
22+
export const useStateValue = () => useContext(StateContext);
23+
24+
/**
25+
* if you have more than one reducer use this function to create root_reducer, we mix all of your reducers into one reducer function
26+
* @param {object} reducers
27+
* @example
28+
const root_reducer = combineReducers({user: reducer_user,items: reducer_items});
29+
*/
30+
export const combineReducers = reducers => {
31+
// First get an array with all the keys of the reducers (the reducer names)
32+
const reducerKeys = Object.keys(reducers);
33+
34+
return function combination(state = {}, action) {
35+
// This is the object we are going to return.
36+
const nextState = {};
37+
38+
// Loop through all the reducer keys
39+
for (let i = 0; i < reducerKeys.length; i++) {
40+
// Get the current key name
41+
const key = reducerKeys[i];
42+
// Get the current reducer
43+
const reducer = reducers[key];
44+
// Get the the previous state
45+
const previousStateForKey = state[key];
46+
// Get the next state by running the reducer
47+
const nextStateForKey = reducer(previousStateForKey, action);
48+
// Update the new state for the current reducer
49+
nextState[key] = nextStateForKey;
50+
}
51+
return nextState;
52+
};
53+
};
54+
55+
// --- propTypes
56+
StateProvider.propTypes = {
57+
/**
58+
* @return {React.Node}
59+
*/
60+
children: PropTypes.node.isRequired,
61+
62+
/**
63+
* Object containing initial state value.
64+
*/
65+
initialState: PropTypes.shape({}).isRequired,
66+
67+
/**
68+
*
69+
* @param {object} state
70+
* @param {object} action
71+
*/
72+
reducer: PropTypes.func.isRequired
73+
};

package.json

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
{
2+
"name": "react-state-provider",
3+
"version": "1.0.0",
4+
"description": "Fully native global state provider using ReactContext and Hooks. You can use this library instead of Redux or Mobx. also you can access state from all of your components and behavior with them using hooks.",
5+
"keywords": [
6+
"react",
7+
"react-native",
8+
"react-context",
9+
"react-state",
10+
"react-state-management",
11+
"react-redux",
12+
"react-hooks",
13+
"react-context-api",
14+
"react-state-hook",
15+
"react-global-state",
16+
"reactjs",
17+
"react-library",
18+
"react-state-provider"
19+
],
20+
"author": "Hamed Taheri",
21+
"license": "MIT",
22+
"main": "index.js",
23+
"repository": {
24+
"type": "git",
25+
"url": "git+https://github.com/intellidev1991/react-state-provider.git"
26+
},
27+
"bugs": {
28+
"url": "https://github.com/intellidev1991/react-state-provider/issues"
29+
},
30+
"homepage": "https://github.com/intellidev1991/react-state-provider#readme",
31+
"dependencies": {
32+
"prop-types": "15.7.2",
33+
"react": "16.8.6",
34+
"react-scripts": "3.0.1"
35+
},
36+
"devDependencies": {
37+
},
38+
"scripts": {
39+
"start": "react-scripts start",
40+
"build": "react-scripts build",
41+
"test": "react-scripts test --env=jsdom",
42+
"eject": "react-scripts eject"
43+
},
44+
"browserslist": [
45+
">0.2%",
46+
"not dead",
47+
"not ie <= 11",
48+
"not op_mini all"
49+
]
50+
}

0 commit comments

Comments
 (0)