![Create React App Officially Deprecated Amid React 19 Compatibility Issues](https://cdn.sanity.io/images/cgdhsj6q/production/04fa08cf844d798abc0e1a6391c129363cc7e2ab-1024x1024.webp?w=400&fit=max&auto=format)
Security News
Create React App Officially Deprecated Amid React 19 Compatibility Issues
Create React App is officially deprecated due to React 19 issues and lack of maintenance—developers should switch to Vite or other modern alternatives.
redux-cycle-middleware
Advanced tools
Bring functional reactive programming to Redux using Cycle.js
Handle redux async actions using Cycle.js.
npm install --save redux-cycle-middleware
Then use createCycleMiddleware()
which takes as first argument your main
Cycle.js function, and second argument the Cycle.js drivers you want to use:
import { createCycleMiddleware } from 'redux-cycle-middleware';
function main(sources) {
const pong$ = sources.ACTION
.filter(action => action.type === 'PING')
.mapTo({ type: 'PONG' });
return {
ACTION: pong$
}
}
const cycleMiddleware = createCycleMiddleware(main, { HTTP: makeHTTPDriver() });
const store = createStore(
rootReducer,
applyMiddleware(cycleMiddleware)
);
Try out this JS Bin.
See a real world example: cycle autocomplete.
Here's how Async is done using redux-observable. The problem is that we still have side-effects in our epics (ajax.getJSON
). This means that we're still writing imperative code:
const fetchUserEpic = action$ =>
action$.ofType(FETCH_USER)
.mergeMap(action =>
ajax.getJSON(`https://api.github.com/users/${action.payload}`)
.map(fetchUserFulfilled)
);
With Cycle.js we can push them even further outside our app using drivers, allowing us to write entirely declarative code:
function main(sources) {
const request$ = sources.ACTION
.filter(action => action.type === FETCH_USER)
.map(action => ({
url: `https://api.github.com/users/${action.payload}`,
category: 'users',
}));
const action$ = sources.HTTP
.select('users')
.flatten()
.map(fetchUserFulfilled);
return {
ACTION: action$,
HTTP: request$
};
}
This middleware intercepts Redux actions and allows us to handle them using Cycle.js in a pure data-flow manner, without side effects. It was heavily inspired by redux-observable, but instead of epics
there's an ACTION
driver observable with the same actions-in, actions-out concept. The main difference is that you can handle them inside the Cycle.js loop and therefore take advantage of the power of Cycle.js functional reactive programming paradigms.
Redux-cycle-middleware ships with two drivers:
ACTION
, which is a read-write driver, allowing to react to actions that have just happened, as well as to dispatch new actions.STATE
, which is a read-only driver that streams the current redux state. It's a reactive counterpart of the yield select(state => state)
effect in Redux-saga.import sampleCombine from 'xstream/extra/sampleCombine'
function main(sources) {
const state$ = sources.STATE;
const isOdd$ = state$.map(state => state.counter % 2 === 0);
const increment$ = sources.ACTION
.filter(action => action.type === INCREMENT_IF_ODD)
.compose(sampleCombine(isOdd$))
.map(([ action, isOdd ]) => isOdd ? increment() : null)
.filter(action => action);
return {
ACTION: increment$
};
}
Here's an example on how the STATE driver works.
Redux-cycle-middleware ships with a combineCycles util. As the name suggests, it allows you to take multiple cycle apps (main functions) and combine them into a single one.
import { combineCycles } from 'redux-cycle-middleware';
// import all your cycle apps (main functions) you intend to use with the middleware:
import fetchReposByUser from './fetchReposByUser';
import searchUsers from './searchUsers';
import clearSearchResults from './clearSearchResults';
export default combineCycles(
fetchReposByUser,
searchUsers,
clearSearchResults
);
You can see it used in the provided example.
Since your main Cycle functions are pure dataflow, you can test them quite easily by giving streams as input and expecting specific streams as outputs. Checkout these example tests. Also checkout the cyclejs/time project, which should work perfectly with redux-cycle-middleware.
Mainly because Cycle.js does not say anything about how to handle state, so Redux, which has specific rules for state management, is something that can be used along with Cycle.js. This middleware allows you to continue using your Redux/React stack, while allowing you to get your hands wet with FRP and Cycle.js.
This middleware doesn't mix Cycle.js with Redux/React at all (like other cycle-redux middlewares do). It behaves completely separately and it's meant to (i) intercept actions, (ii) react upon them functionally and purely, and (iii) dispatch new actions. So you can build your whole app without this middleware, then once you're ready to do async stuff, you can plug it in to handle your async stuff with Cycle.
You should think of this middleware as a different option to handle side-effects in React/Redux apps. Currently there's redux-observable and redux-saga (which uses generators). However, they're both imperative and non-reactive ways of doing async. This middleware is a way of handling your side effects in a pure and reactive way using Cycle.js.
FAQs
Bring functional reactive programming to Redux using Cycle.js
The npm package redux-cycle-middleware receives a total of 3 weekly downloads. As such, redux-cycle-middleware popularity was classified as not popular.
We found that redux-cycle-middleware demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Create React App is officially deprecated due to React 19 issues and lack of maintenance—developers should switch to Vite or other modern alternatives.
Security News
Oracle seeks to dismiss fraud claims in the JavaScript trademark dispute, delaying the case and avoiding questions about its right to the name.
Security News
The Linux Foundation is warning open source developers that compliance with global sanctions is mandatory, highlighting legal risks and restrictions on contributions.