ghostagent151
ghostagent151

Reputation: 1426

How can I add a console log in this function?

Pretty straight forward question, I'm just trying to get a console log in this es6 function.

const testLinesSelector = state => state.tests.testLines || [];

I've tried:

const testLinesSelector = state => { 
  console.log('INSIDE THE SELECTOR ===> ');
  return {state.tests.testLines || [] }
};

Upvotes: 1

Views: 48

Answers (2)

Dai
Dai

Reputation: 155598

Another approach you might consider automating your code using Function.name - though this requires using a named function (a la function foobar() { ... }). There are techniques to get the "name" of an anonymous function or arrow-function in JavaScript but they're more involved and may not be compatible with the "established patterns in the code" you're working with.

But here's how you can do it with named functions:

function annotate( func ) {
    return ( ...args ) => {
        console.log( func.name ); // `Function.name` is supported in ES6.
        return func( ...args );
    };
}

const testLinesSelector = annotate( function testLinesSelector( state ) { return state.tests.testLines || [] } );

Upvotes: 0

Dai
Dai

Reputation: 155598

Remove the braces in your return statement, like so:

const testLinesSelector = state => { 
  console.log('INSIDE THE SELECTOR ===> ');
  return state.tests.testLines || [];
};

As a side-note, I noticed a lot of ES6 code uses function-variables or function-properties instead of just functions - is there a reason you don’t do this instead?

function testLinesSelector( state ) { 
  console.log('INSIDE THE SELECTOR ===> ');
  return state.tests.testLines || [];
};

Upvotes: 1

Related Questions