All Projects → kutlugsahin → redux-saga-callback

kutlugsahin / redux-saga-callback

Licence: MIT license
redux-saga helper functions to await dispatched actions

Programming Languages

javascript
184084 projects - #8 most used programming language
typescript
32286 projects

Projects that are alternatives of or similar to redux-saga-callback

React Universal Saga
Universal React Starter Kit ft. Redux Saga
Stars: ✭ 184 (+868.42%)
Mutual labels:  redux-saga
handwritten-digit-recognition-tensorflowjs
In-Browser Digit recognition with Tensorflow.js and React using Mnist dataset
Stars: ✭ 40 (+110.53%)
Mutual labels:  redux-saga
head-start
A boilerplate for React applications with an Express backend with a simple user registration/login
Stars: ✭ 24 (+26.32%)
Mutual labels:  redux-saga
Eth Hot Wallet
Ethereum wallet with erc20 support / web wallet - built using react, web3, eth-lightwallet
Stars: ✭ 205 (+978.95%)
Mutual labels:  redux-saga
Neos Ui
Neos CMS UI written in ReactJS with Immutable data structures.
Stars: ✭ 238 (+1152.63%)
Mutual labels:  redux-saga
LuaCSP
Communicating Sequential Processes in Lua
Stars: ✭ 40 (+110.53%)
Mutual labels:  yield
Next Redux Saga
redux-saga HOC for Next.js
Stars: ✭ 178 (+836.84%)
Mutual labels:  redux-saga
fractal-component
A javascript library that can help you to encapsulate decoupled UI component easily
Stars: ✭ 13 (-31.58%)
Mutual labels:  redux-saga
Arc
React starter kit based on Atomic Design
Stars: ✭ 2,780 (+14531.58%)
Mutual labels:  redux-saga
react-redux-hackernews
React Redux Hackernews Application
Stars: ✭ 19 (+0%)
Mutual labels:  redux-saga
Digag Pc React
digag pc website based on react.
Stars: ✭ 209 (+1000%)
Mutual labels:  redux-saga
Redux Saga Thunk
Dispatching an action handled by redux-saga returns promise
Stars: ✭ 212 (+1015.79%)
Mutual labels:  redux-saga
react-template
My starting template for most react apps
Stars: ✭ 23 (+21.05%)
Mutual labels:  redux-saga
Reactotron
A desktop app for inspecting your React JS and React Native projects. macOS, Linux, and Windows.
Stars: ✭ 13,337 (+70094.74%)
Mutual labels:  redux-saga
react-ts-sample
sample tech stack with react and typescript
Stars: ✭ 26 (+36.84%)
Mutual labels:  redux-saga
Todo Redux Saga
Todo app with Create-React-App • React-Redux • Redux-Saga • Firebase • OAuth
Stars: ✭ 184 (+868.42%)
Mutual labels:  redux-saga
etoolbox
Collection of web developer utilities packaged as a desktop app. Technologies: React, Typescript, Redux, Axios, React-Query, Electron
Stars: ✭ 95 (+400%)
Mutual labels:  redux-saga
react-truffle-metamask
Build an DApp using react, redux, saga, truffle, metamask
Stars: ✭ 25 (+31.58%)
Mutual labels:  redux-saga
kotlin-coroutines-jdbc
A library for interacting with blocking JDBC drivers using Kotlin Coroutines.
Stars: ✭ 40 (+110.53%)
Mutual labels:  higher-order-functions
BOHM1.1
Bologna Optimal Higher-Order Machine, Version 1.1
Stars: ✭ 45 (+136.84%)
Mutual labels:  higher-order-functions

redux-saga-callback

In a normal flow of a redux-saga application there might be some cases that you want get notified when a saga triggered by a dispatched action is completed. The purpose of this library is to provide some helper functions to achieve that functionalities.

Install

npm i redux-saga-callback

Usage

import { putWait, withCallback } from 'redux-saga-callback';

Wrap your saga generator with withCallback

takeEvery('FETCH_USERS', withCallback(fetchUsers));

Defined callback in your action if you need

dispatch({type: 'FETCH_USERS', onComplete: ({error, cancelled, data}) => {

}})

In saga you can wait for it (no callback definition is needed with putWait)

const users = yield putWait({type: 'FETCH_USERS'});

API

withCallback

This is a higer order saga which will call the onComplete callback property of the action if defined.

onComplete

  • error: error which is thrown by the saga
  • cancelled: Will be true if the saga is cancelled
  • data: the data returned by the saga
function onComplete({ error, cancelled, data }) {
    /* handle callback */
}
function* fetchUsers() {
    const users = yield fetch('/users');

    // put users to store
    yield put(putUsers(users));

    // returned value will be passed to onComplete function as parameter
    // Exceptions will be handled by the withCallback and will also be passed to onComplete
    return users;
}

export function*(){
    yield all([
        takeEvery('FETCH_USERS', withCallback(fetchUsers))
    ])
}

// userSaga.js
// Component to list users
export const Users = () => {
    const [isLoading, setIsLoading] = useState(true);
    const [users, setUsers] = useState([]);

    const dispatch = useDispatch();

    function onUsersFetchCompleted({ error, cancelled, data }) {
        setIsLoading(false);
        if (!err && !cancelled) {
            setUsers(data);
        }
    }

    useEffect(() => {
        dispatch({
            type: 'FETCH_USERS',
            onComplete: onUsersFetchCompleted,
        });
    }, []);

    return isLoading ? (
        'Loading'
    ) : (
        <ul>
            {users.map(user => (
                <li>{user.name}</li>
            ))}
        </ul>
    );
};

// Users.jsx

putWait

An effect that dispatches the action (same as put effect) which you can yield and wait for that saga to be completed only if the saga is created using withCallback higher order saga

Example

function* loadCurrentUserData() {
    const currentUserId = getCurrentUserId();

    let users = yield select(state => state.users);

    if(!users) {
        // waits until fetchUsers saga is completed
        // fetchUser saga is defined in userSaga.js above
        users = yield putWait({type: 'FETCH_USERS'});
    }

    return users.find(p => p.id === currentUserId);
}

export function*(){
    yield all([
        takeEvery('LOAD_CURRENT_USER', fetchUsers)
    ])
}

// userData.js
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].