Store setup with simpleWebRTC - reactjs

I'm trying to integrate simpreWebRTC to my React-Redux project, but the library has their own redux store and the documentation says this:
"The provided createStore function makes a basic Redux
store useful for getting things started. If you want to
make your own, import reducer from '#andyet/simplewebrtc' and
be sure to assign it to simplewebrtc in the top level of
your state object."
I've tried several approaches but nothing works, any idea? what I'm missing here?
Thanks
This is the code that I have so far:
store.js
import {createStore, applyMiddleware} from 'redux'
import rootReducer from './reducers/index'
import thunk from 'redux-thunk';
export default createStore(rootReducer, applyMiddleware(thunk));
const store = createStore(rootReducer);
console.log(store.getState());
./reducers/index.js
import {combineReducers} from 'redux'
import {reducer as simplewertc} from '#andyet/simplewebrtc'
import liveRoomReducer from './liveRoomReducer'
export default combineReducers({simplewertc, liveRoomReducer});
./reducers/liveRoomReducer.js
const initialState = {
test : 'test'
};
export default function liveRoomReducer(state=initialState, action) {
return state;
};
I'm logging the store state in the console and is showing simplewebrtc on it:
And still showing this error:

Creating your own store with thunk middleware and using combineReducers should do the trick:
import {combineReducers} from 'redux';
import {reducer as simplewebrtc} from '#andyet/simplewebrtc';
import reducer1 from 'path/to/your/reducer1';
import reducer2 from 'path/to/your/reducer2';
export default combineReducers({simplewebrtc, reducer1 , reducer2});
If that isn't working for you please provide what error is showing up if any and some example code of how you create your redux store and root reducer.
Edit: After seeing the updated question with code, we found the problem was in a typo when importing the reducer.

Related

why can't I export 'combineReducers' (imported as 'combineReducers') was not found in 'react-redux'

Here is my main js code and why combine all reducer
cartredux\src\redux\reducers\main.js
import {combineReducers} from "react-redux";
import { cartreducer } from "./reducer";
const rootred = combineReducers({
cartreducer
});
export default rootred;
As a comment pointed out: the combineReducers function is part of either the #reduxjs/toolkit or redux packages, because it's about the non-UI portion of the logic. It's not part of the react-redux package, which is specifically about React UI integration with Redux.
Make sure to Import combineReducers from redux.
import {combineReducers} from "redux"

what is the difference between Redux CombineReducers and Redux-toolkit ConfigureStore

import { configureStore } from "#reduxjs/toolkit";
import testSlice from "./testSlice";
import {combineReducers} from "redux";
const rootReducer = combineReducers({test: testSlice})
export const store = configureStore({
reducer: rootReducer,
});
Which one is better? for performance and use purpose. Which is good to use?
They are totally different things.
The reducer option is an object of slice reducers, like {users : usersReducer, posts : postsReducer}, configureStore will automatically create the root reducer by passing this object to the Redux combineReducers utility. See here
RTK configureStore setup the redux store configuration, not only reducer, but also middlewares, dev tools, preloaded state, and enhancers.
The Redux combineReducers helper function turns an object whose values are different reducing functions into a single reducing function

It looks like you are passing several store enhancers to createStore() react-thunk

I was experimenting with redux for a bit and came across a problem, I found the solution( here: React Redux - Error passing several store enhancers to createStore()) however this is not the solution I wanted. Basically I have the same problem as the person asking the question basically when creating the redux store we did this:
import { createStore, applyMiddleware } from "redux";
import { composeWithDevTools } from "redux-devtools-extension";
import reduxThunk from 'redux-thunk';
import rootReducer from "./reducers";
const store = createStore(
rootReducer,
composeWithDevTools(applyMiddleware(reduxThunk)),
window.__REDUX_DEVTOOLS_EXTENSION__ && window.__REDUX_DEVTOOLS_EXTENSION__()
);
But the code above is not the correct way of creating the store, apparently you should create the store is like this:
import { createStore, compose, applyMiddleware } from "redux";
import reduxThunk from "redux-thunk";
import rootReducer from "./reducers";
const composeEnhancer = window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__ || compose;
const store = createStore(
rootReducer,
composeEnhancer(applyMiddleware(reduxThunk))
);
However in the solution above I am not using the composeWithDevTools module which is what I wanted to use. Is there a way to use composeWithDevTools in this case and is it necessary to use composeWithDevTools?
Today you should be using our official Redux Toolkit package to write your Redux logic, and in particular, RTK's configureStore API.
configureStore automatically sets up the Redux DevTools Extension for you, automatically turns on the thunk middleware, and also makes it very easy to add additional store enhancers if desired.
The example you're showing would simply be:
const store = configureStore({
reducer: rootReducer
});
and the behavior is exactly the same as what you showed.

How can we import index.js by default ,and reducer which is not inside index.js file

I have no class named or function reducers in my code but explanation says it takes index.js by default in index.js there is class or function named reducer i am having no errors and compiling fine how?
I have used create-react -app to create app
The following is index.js code which is reducers
import { combineReducers } from 'redux';
import movies from './movies_reducer';
const rootReducer = combineReducers({
movies
})
export default rootReducer
The following is the index.js file where i am using index.js from reducer
import reducers from './reducers'
const createStoreWithMiddleware = applyMiddleware()(createStore)
ReactDOM.render(
<Provider store={createStoreWithMiddleware()}>
<App />
</Provider>,
How the above code works please explain
Importing a directory is syntactical sugar for importing the index.js file inside the directory. Provided you have a directory named reducers and a file named index.js inside it, the following imports are equivalent.
import reducers from './reducers'
import reducers from './reducers/index'
import reducers from './reducers/index.js'
In addition, default exports can be imported with any name.
export default rootReducer
// file.js - the following import statements import the same rootReducer
import reducers from './reducers'
import rootReducer from './reducers'
import anyName from './reducers'

Configuration store variation with React/redux and thunkMiddleware

I've been through many examples to understand how Redux-Thunk works and most the time the store is configured in a various way. I guess there's the old way or the new way but I'm kind of lost. Here is three pattern that I've identified. If someone can explain me the differences between them :
The simple way :
import { createStore, applyMiddleware } from 'redux';
import thunkMiddleware from 'redux-thunk'
import createLogger from 'redux-logger'
const loggerMiddleware = createLogger()
const store = createStore(rootReducer, applyMiddleware( thunkMiddleware, loggerMiddleware));
the Official Reddit Async Exemple way (here) :
import { createStore, applyMiddleware } from 'redux'
import thunkMiddleware from 'redux-thunk'
import createLogger from 'redux-logger'
const loggerMiddleware = createLogger()
export default function configureStore(preloadedState) {
return createStore(
rootReducer,
preloadedState,
applyMiddleware(
thunkMiddleware,
loggerMiddleware
)
)
}
the old way ?
import {compose, createStore, applyMiddleware } from 'redux';
import thunkMiddleware from 'redux-thunk';
const createAppStore = compose(
applyMiddleware(thunkMiddleware)
)(createStore);
export default function configureStore(initialState){
const store = createAppStore(rootReducer, initialState);
return store;
};
From there I have at least four questions :
Do we still need to use compose ? I only find it in "old" exemple ?
Is there any differences between import ReduxThunk from
'redux-thunk' and import thunkMiddleware from 'redux-thunk' ?
Does the simple way is also correct ?
I don't understand the preloadedState pattern from the Reddit Async Exemple.
thanks.
The signature for createStore is createStore(reducer, [preloadedState], [enhancer]).
preloadedState is an initial state you load before you initialize your app. For instance, if you prerender your first page on a server and want to pass app state as a JSON inside your HTML. Sometimes you need to fetch this state asynchronously that is where the second example is useful. You fetch your state and create a store using that state in the callback of your ajax call.
The third argument.
enhancer is a higher-order function that composes a store creator to return a new, enhanced store creator. applyMiddleware is a store enhancer shipped with redux. If you want to combine multiple store enhancers you need to use compose function.
For instance, redux-devtools-extension for chrome is an enhancer so to use it in your app you would need compose function
let store = createStore(reducer, initialState, compose(
applyMiddleware(...middleware),
window.devToolsExtension ? window.devToolsExtension() : f => f
));
When you import something from 'redux-thunk' you use default export so you can name your variable as you want. It doesn't matter.
A simple way is also correct if you don't need anything fancy it would work just fine.

Resources