Skip to content

Commit d96d068

Browse files
committed
README
1 parent c66ca0f commit d96d068

File tree

2 files changed

+105
-2
lines changed

2 files changed

+105
-2
lines changed

README.md

Lines changed: 103 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,112 @@
22

33
# redux-saga-rn-alert
44

5+
Ever wanted to use Alert.alert() with callbacks within a side-effect or generator function? This library help along!
6+
7+
It allows us to show a typical alert-modal while we can pass callbacks
8+
for the user-action the redux-saga-way with `yield put()` or `yield put()`.
9+
Other side-effects like `fork` or `spawn` aren't implemented yet. Feel
10+
free to contribute if you need it!
11+
512
# Installation
6-
```
13+
14+
```bash
715
yarn add redux-saga-rn-alert
816
```
17+
or
18+
```bash
19+
npm install redux-saga-rn-alert --save
20+
```
21+
22+
# Setup
23+
24+
In the root reducer add the alert-reducer. For instance:
25+
26+
**reducer.js:**
27+
28+
```javascript
29+
import { alertReducer } from 'redux-saga-rn-alert';
30+
31+
const appReducer = combineReducers({
32+
appStates,
33+
routes,
34+
...
35+
alertReducer
36+
});
37+
```
38+
39+
In the root saga spawn the channel watcher:
40+
41+
**saga.js**
42+
43+
```javascript
44+
export default function * rootSaga() {
45+
yield [
46+
// ... all your sagas here
47+
spawn(watchAlertChannel),
48+
];
49+
}
50+
```
951

1052
# Usage
1153

54+
The alert-function has the same signature as the official [alert-method](https://facebook.github.io/react-native/docs/alert.html#alert)
55+
of reacht-native.
56+
57+
```javascript
58+
static alert(title, message?, buttons?, options?) {}
59+
```
60+
61+
## Example 1
62+
63+
Show an alert with 2 Buttons and put some actions:
64+
65+
```javascript
66+
const buttons = [
67+
{text: 'Cancel', put: {type: ACTIONS.S_CANCEL_EDIT}},
68+
{text: 'OK', call: RouterActions.pop},
69+
]
70+
71+
yield call(alert, 'Error', 'Foobar message', buttons)
72+
```
73+
74+
In this example an alert-box will be shown with two buttons "Cancel"
75+
and "ok". If the user taps on "cancel", a ```yield put()``` will be
76+
executed with a user-defined action, while "ok" raises a ```yield call````
77+
to the pop-method of the Router from [react-native-router-flux] (https://github.com/itinance/react-native-router-flux).
78+
79+
Instead of executing a "plain" function, you can also call a method passing
80+
arguments:
81+
82+
## Example 2
83+
84+
```javascript
85+
const buttons = [
86+
{text: 'Cancel', put: {type: ACTIONS.S_CANCEL_EDIT}},
87+
{text: 'OK', call: {method: myMethod, args: {name: '', street: ''}},
88+
]
89+
90+
yield call(alert, 'Error', 'Happy to call alert with callbacks within a generator function', buttons)
91+
```
92+
93+
## Example 3
94+
95+
It is also allowed to pass an array of several side effects:
96+
97+
```javascript
98+
const buttons = [
99+
{text: 'Cancel', actions:
100+
[
101+
{put: {type: ACTIONS.S_CANCEL_EDIT}},
102+
{call: RouterActions.pop},
103+
],
104+
},
105+
{text: 'OK', call: RouterActions.pop},
106+
]
107+
108+
yield call(alert, 'Error', 'Foobar message', buttons)
109+
```
110+
111+
In this example, the callback-function of the Cancel-button will
112+
first "yield put" an action to our reducers and then call the pop()-method
113+
of the router.

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "redux-saga-rn-alert",
3-
"version": "0.0.1",
3+
"version": "1.0.0",
44
"description": "Alert-support for sagas and side-effects",
55
"main": "index.js",
66
"scripts": {
@@ -14,6 +14,7 @@
1414
"keywords": [
1515
"react-native-component",
1616
"redux-saga",
17+
"redux-saga-channels",
1718
"alert",
1819
"side-effect"
1920
],

0 commit comments

Comments
 (0)