Access Recovery Features

I would prefer the function to be opened from a file .js, inside this function I would prefer to have access to the variables in the repository.

Code snippet: -

import { connect } from 'react-redux';

function log(logMessage) {
    const {environment} = this.props;
    console.debug('environment' + environment + logMessage ); 
    ....
}

function mapStateToProps(state) {
    return {
        environment : state.authReducer.environment
    };
}

export default function connect(mapStateToProps)(log);

I have many components that attach a class through connect, can I attach functions through connect()?

+13
source share
4 answers

Change 1

Some_File.js

import store from './redux/store.js';

function aFunction(){
   var newState =store.getState();
   console.log('state changed');
}

store.subscribe(aFunction)

I assume that you have created storage and gearboxes as the reduction is expected.

~~~~~~~~~~~~~~~~

Original answer begins

, , , , , , . .

Class XYZ extends React.Component{
     componentWillReceiveProps(props){
       //in your case this.props.storeCopy is redux state.
      //this function will be called every time state changes
     }

     render(){
        return null;
     }
}



function mapStateToProps(state) {
    return {
        storeCopy : state
    };
}

export default function connect(mapStateToProps)(XYZ);

- , provider, , componentWillReceiveProps , .

+11

, connect , .

logger , , , , , .

, :

import createLogger from 'redux-logger';

const store = createStore(
  reducer,
  applyMiddleware(logger)
);

redux React Dev Tools, Chrome, React Dev Tools Extension.

let store = createStore(reducer, window.devToolsExtension && window.devToolsExtension());

, , , , .

0

. , :

  const mapDispatchToProps = (dispatch) => {
     return {
       testFunction: (param1) => dispatch(testFunction(param1)),
       testFunction1: () => dispatch(testFunction1())
    };
 };

 export default function connect(mapStateToProps, mapDispatchToProps)(log);
0

If you have a purely functional component, then you can access the redundant state directly, like this

import store './redux/store';

function getStoreDetails() {
   console.log(store.getState());
}
0
source

Source: https://habr.com/ru/post/1651462/


All Articles