Skip to content

Instantly share code, notes, and snippets.

@awinogradov
Last active January 31, 2017 18:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save awinogradov/e8f9ea51a9c44b6cc0ae911e3851c2ea to your computer and use it in GitHub Desktop.
Save awinogradov/e8f9ea51a9c44b6cc0ae911e3851c2ea to your computer and use it in GitHub Desktop.
Connect to state (bem-react-core)
  • blocks
    • Screen
      • Actions
        • Screen-Actions.js
      • Reducer
        • Screen-Reducer.js
      • Screen.js
    • Root
      • Root.js
  • state
    • index.js
  • lib
    • connector.js
import React from 'react';
import { bindActionCreators } from 'redux';
import { connect } from 'react-redux';
export default function connectToState(inst) {
const Connected = connect(inst.stateToProps, dispatch => {
return {
...bindActionCreators(inst.actions || {}, dispatch),
dispatch
};
})(inst);
return Connected;
};
import { createStore, applyMiddleware, compose, combineReducers } from 'redux';
import thunk from 'redux-thunk';
import Screen from 'b:Screen';
const actionCreators = {
...Screen.actions
};
const rootReducer = combineReducers({
screen: Screen.reducer
});
const enhancer = compose(
applyMiddleware(thunk),
window.devToolsExtension ?
window.devToolsExtension({ actionCreators }) :
noop => noop
);
export default function configureStore(initialState) {
const store = createStore(rootReducer, initialState, enhancer);
if (window.devToolsExtension) {
window.devToolsExtension.updateStore(store);
}
if (module.hot) {
store.replaceReducer(rootReducer);
}
return store;
}
import React from 'react';
import { decl } from 'bem-react-core';
import Screen from 'b:Screen';
const СScreen = connect(Screen);
export default decl({
block: 'Root',
content() {
return <СScreen />;
}
});
import {createAction} from 'redux-actions';
export const select = createAction('SELECT_SCREEN');
import { handleActions } from 'redux-actions';
export default handleActions({
SELECT_SCREEN: (state, action) => ({
name: action.payload
})
}, {});
import React from 'react';
import { decl } from 'bem-react-core';
import * as actions from './Actions/Screen-Actions';
import reducer from './Reducer/Screen-Reducer';
export default decl({
block: 'Screen'
}, {
actions,
reducer,
stateToProps: state => ({
current: state.screen.name
})
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment