How to use the createStore function from redux

Find comprehensive JavaScript redux.createStore code examples handpicked from public code repositorys.

redux.createStore is a function in the Redux library that creates a Redux store that holds the complete state tree of the app.

4866
4867
4868
4869
4870
4871
4872
4873
4874
var middlewares = [sagaMiddleware].concat(middleWare // isWeb ? [routerMiddleware(History)] : [],
); // eslint-disable-next-line no-underscore-dangle

var _enhancers = [redux.applyMiddleware.apply(void 0, _toConsumableArray(middlewares))].concat(_toConsumableArray(enhancers));

var store = redux.createStore(createReducer({}), initialState, composeEnhancers.apply(void 0, _toConsumableArray(_enhancers))); // Extensions

store.runSaga = sagaMiddleware.run;
store.injectedReducers = {}; // Reducer registry
fork icon6
star icon9
watch icon0

+ 3 other calls in file

36
37
38
39
40
41
42
43
44
45
      return state
  }
}

const { middleware } = createMiddleware()
const store = createStore(
  reducer,
  initialState,
  applyMiddleware(middleware)
)
fork icon0
star icon13
watch icon41

+ 407 other calls in file

How does redux.createStore work?

redux.createStore is a function in the Redux library that creates a Redux store to manage the application state. When you call createStore, you pass in a reducer function that defines how state is updated based on actions, and an optional initial state. The createStore function returns a store object that provides methods to dispatch actions and subscribe to changes in the state. When an action is dispatched, the store applies the reducer to the current state and the action, producing a new state, which is then broadcasted to all subscribers.

186
187
188
189
190
191
192
193
194
195
196
197
198


    return newState;
};


var mainReducer = Redux.combineReducers({auth: auth, table: table, filter: filter, modal: modal, apps: apps, div: div, panel: panel, alert: alert, form: form});
var store = Redux.createStore(mainReducer);


var Home = require('./tabs/home');
var Overview = require('./tabs/overview');
var Hosts = require('./tabs/hosts');
fork icon2
star icon0
watch icon0

+ 3 other calls in file

25
26
27
28
29
30
31
32
33
34
      storage: localforage,
    },
    appReducers
  );
}
let store = createStore(
  rootReducers,
  preloadedState,
  composeEnhancers(applyMiddleware(promiseMiddleware, ReduxThunk))
);
fork icon0
star icon0
watch icon1

Ai Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
import { createStore } from "redux";

// Define the reducer function
function counterReducer(state = { count: 0 }, action) {
  switch (action.type) {
    case "INCREMENT":
      return { count: state.count + 1 };
    case "DECREMENT":
      return { count: state.count - 1 };
    default:
      return state;
  }
}

// Create the store
const store = createStore(counterReducer);

// Get the current state of the store
console.log(store.getState()); // { count: 0 }

// Dispatch an action to the store
store.dispatch({ type: "INCREMENT" });

// Get the updated state of the store
console.log(store.getState()); // { count: 1 }

239
240
241
242
243
244
245
246
247
248
    } else {
      // no route provided any reducer
      reducer = x => x;
    }

    options.store = createStore(reducer, initialState);
  }

  return options.store;
}
fork icon0
star icon0
watch icon1

237
238
239
240
241
242
243
244
245
246

const framework = new lib.FrameworkLib({
  subApp: {
    __redux: true,
    Component,
    reduxCreateStore: initState => Redux.createStore(x => x, initState),
    prepare: () => ({ test: "foo bar" })
  },
  subAppServer: { attachInitialState: false },
  options: { serverSideRendering: true },
fork icon0
star icon0
watch icon1

+ 7 other calls in file

12
13
14
15
16
17
18
19
20
21
22
23
        console.log("object 이니?")
        return next(action)
    }
}


const store = createStore(rootReducer, applyMiddleware(createThunkMiddleware()))


// const api = () => {
//     store.dispatch({ type: "increment" })
// }
fork icon0
star icon0
watch icon1

+ 2 other calls in file

0
1
2
3
4
5
6
7
8
9
10
11
12
'use strict';


var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };


var _require = require('redux'),
    createStore = _require.createStore;


var Game = require('./Game.react');
var React = require('react');
var ReactDOM = require('react-dom');
fork icon0
star icon0
watch icon2

+ 9 other calls in file

119
120
121
122
123
124
125
126
127
128
129
130
  });
  return neighborTile;
};


// all state is here
const store = createStore(actionHandler);
window.store = store;


// helper to dispatch a move action for a key iff the key is an arrow key
// action creator helpers are prefixed with bound- and call store.dispatch
fork icon0
star icon0
watch icon2

+ 7 other calls in file

19
20
21
22
23
24
25
26
27
28
29
30
        return {
        counter : state.counter //if no action,initial state will be returned
    }
}


const store = redux.createStore(counterReducer)//creating store and it takes reducer as input


const counterSubscriber = () => {
    const latestState = store.getState() //creating subscription
    console.log(latestState)
fork icon0
star icon0
watch icon1

58
59
60
61
62
63
64
65
66
const composeEnhancers =
(typeof window !== 'undefined' &&
   window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__) ||
compose;

 export const store = createStore(
  videoGame_state, composeEnhancers(applyMiddleware(thunk.default))
);

fork icon0
star icon0
watch icon1

+ 7 other calls in file

26
27
28
29
30
31
32
33
34
35
36
37
  compB: 10,
  compC: null
};


// store
const store = createStore(reducer, initialState);
store.subscribe(() => console.log(store.getState()));


// action
const changeComp = (type, data) => ({ type, data });
fork icon0
star icon0
watch icon1

+ 4 other calls in file

8
9
10
11
12
13
14
  [mealsSlices.name]: mealsSlices.reducer,
  [basketSlice.name]: basketSlice.reducer,
  [uiSlice.name]: uiSlice.reducer,
});


export const store = createStore(rootReducer, applyMiddleware(thunk));
fork icon0
star icon0
watch icon0

82
83
84
85
86
87
88
89
90
91
92
93
94
const rootReducer = combineReducers({
  cake: cakeReducer,
  iceCream: iceCreamReducer,
});


const store = createStore(rootReducer);


console.log(`Initial State : `, store.getState());


const unsubscribe = store.subscribe(() => {
fork icon0
star icon0
watch icon0