npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

framework-x-redux

v1.0.0-alpha.2

Published

A bridge from Redux to framework-x.

Downloads

82

Readme

framework-x-redux

Bridge from Redux to Framework-X

Installation

npm i framework-x-redux

Usage

import { frameworkXRedux, makeFrameworkXMiddleware } from 'framework-x-redux'
import { createStore as frameworkXCreateStore } from 'framework-x'
import { applyMiddleware, createStore } from 'redux'
import reducer from './reducers'


const { env } = frameworkXCreateStore()
const frameworkXMiddleware = makeFrameworkXMiddleware(env)

const store = createStore(
  reducer,
  applyMiddleware(frameworkXMiddleware)
)

const { dispatch } = frameworkXRedux(env, store, reducer)

How it works

The library uses Redux middleware and replaceReducer to forward events to framework-x and synchronize the state of both frameworks. Whenever a dispatched action.type has a registered framework-x event handler, it's called with the action's arguments. The framework-x state is merged with Redux's and side effects are executed.

API

frameworkXRedux

arguments:

  • env - from Framework-X createStore
  • store - from Redux createStore
  • reducer - Root reducer

returns:

  • dispatch - Redux dispatch with Framework-X's dispatch signature

Computes the next state as the result of calling a Redux reducer with the current action and state from Framework-X. Allows arbitrary keys on the state to be set by Framework-X without pre-initialization or a corresponding reducer. Favors the values of keys returned by the reducer if there is a conflict. The resulting state is accessible from Redux and Framework-X through their normal APIs.

Example:

import { applyMiddleware, createStore } from 'redux'
import { composeWithDevTools } from 'redux-devtools-extension'
import { frameworkXRedux, makeFrameworkXMiddleware } from 'framework-x-redux'
import reducer from './reducers'
import { createStore as frameworkXCreateStore } from 'framework-x'

const { env } = frameworkXCreateStore()

const store = createStore(
  reducer,
  composeWithDevTools(
    applyMiddleware(
      makeFrameworkXMiddleware(env),
    )
  )
)
const { dispatch } = frameworkXRedux(env, store, reducer)

makeFrameworkXMiddleware

arguments:

  • env - from Framework-X createStore

returns:

  • Redux middleware

Synchronizes Framework-X db with the state returned by the root reducer for the current action.

Processes event handlers registered for action.type and the effects they return per normal Framework-X semantics.

Allows events dispatched from the view or other middleware to be handled by Redux, Framework-X, or both.

dispatch from event handlers has the following framework-x-redux-specific behavior:

If Framework-X has no registered handlers for the event:

  • We assume you've dispatched an event Redux is capable of handling. The event will be dispatched as a Redux action once the state transition is complete.

If Framework-X is registered to handle it:

  • The event will not be dispatched to Redux: We assume you're communicating with another framework-x event handler. The dispatch is handled within Framework-X and not re-dispatched to Redux.

Example:

import {  makeFrameworkXMiddleware } from 'framework-x-redux'
import { createStore as frameworkXCreateStore } from 'framework-x'

const { env } = frameworkXCreateStore()

const frameworkXMiddleware = makeFrameworkXMiddleware(env)

component

arguments:

  • name - Name of component to show in stacktrace, React Dev Tools
  • subscriptionFn - Function that accepts the global state and returns props. If no third argument provided, this is assumed to be a renderFn
  • renderFn - Function that receives props and returns JSX/React elements

returns:

  • ConnectedComponent

Same API and behavior as component from Framework-X but integrates with react-redux Provider. Calls the component with props obtained from selectors, props passed to the parent component, and Redux's dispatch function that uses Framework-X's (eventName, args?) signature.

Example:

import { Provider } from 'react-redux'
import { createSub } from 'framework-x'
import { component } from 'framework-x-redux'

const MyComponent = component('MyComponent', createSub({
  mySelector1,
  mySelector2
}), ({ mySelector1, mySelector2, myParentProps }) => {
  return  (
    <div>{...}</div>
  )
})

const Example  = () => 
  <div>
    <MyComponent myParentProps={42}  />
  </div>
  
const App = () => 
 <Provider>
   <Example />
 </Provider>

Use cases

Adding new feature to an existing Redux application

You may want to add Framework-X to a Redux application without modifying the existing codebase. The API was designed with this in mind. All that's required is creating a Framework-X store and using its env to register frameworkXRedux at application start along with the provided middleware. From there you're free to write event handlers and effects that pertain to the new feature and write components for them. If you need to read from other parts of your app to develop the new feature, your Redux state is merged with your Framework-X state, so you can access it from event handlers, effects, selectors, etc. as needed.

Using event handlers instead of reducers

The example in react-redux-realworld-example-app replaces the editor reducer with event handlers. Supposing the editor reducer not been written beforehand, this example shows how to use event handlers instead of reducers for a view layer that uses Redux. Existing Redux middleware for async API requests and components that use react-redux are preserved.

Dispatching Redux actions from Framework-X event handlers

Events dispatched from event handlers are handled by either Framework-X or Redux, but not both. This means you can use Framework-X the same as you might use other middleware like redux-saga to dispatch an action/event to Redux in response to another one, like a route change or API success. If the event you dispatch has a handler registered with Framework-X, it will be handled there only.