1.0.1 • Published 5 years ago

react-flux-hook v1.0.1

Weekly downloads
1
License
MIT
Repository
github
Last release
5 years ago

React Flux Hook

This repo is derived from the following Medium Article.

Enhance Your App with Global State

import { StateProvider } from 'react-flux-hook'

const App = () => {
	const initialState = {
		theme: { primary: 'green' },
	}

	const reducer = (state, action) => {
		switch (action.type) {
			case 'changeTheme':
				return {
					...state,
					theme: action.newTheme,
				}

			default:
				return state
		}
	}

	return (
		<StateProvider initialState={initialState} reducer={reducer}>
			// App content ...
		</StateProvider>
	)
}

Then Use and Update the State Inside Your App

import { useStateValue } from 'react-flux-hook';

const ThemedButton = () => {
  const [{ theme }, dispatch] = useStateValue();
  return (
    <Button
      primaryColor={theme.primary}
      onClick={() => dispatch({
        type: 'changeTheme',
        newTheme: { primary: 'blue'}
      })}
    >
      Make me blue!
    </Button>
  );
}```