Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

84
Views
Get items from localStorage and export them

I have a function where it takes fields stored in state and returns them if they exist.

export const hasNameAndTokenSelector = (state) => {  
    const { fields } = baseInputsSelector(state);
    const hasName = !isNil(prop('name')(fields));
    const hasToken = !isNil(prop('token')(fields));
    
    return hasName && hasToken;
};

But now I need to use this same function to get items stored in localStorage. I'm trying to do the following:

export const hasNameAndTokenSelector = (state) => {
    if (localStorage.getItem('inputs') !== null) {
      const localInputs = JSON.parse(localStorage.getItem('inputs'));
  
      const hasToken = localInputs[0].token;
      const hasName = localInputs[0].name;
  
      return hasName && hasToken;
    }
  };

The format of localInputs is:

{
   "name":"John Doe",
   "token":"string123-9882",
}

But it doesn't work, I believe it's because I need the state parameter and the return is inside an if, but I don't know the correct way to proceed.

How should I get these stored items and return them? I need to use hasNameAndTokenSelector in another file.

Thanks!!

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

From the above comment ...

"... code review ... since the function's name is hasNameAndTokenSelector it is expected to always return a boolean value, neither once [the undefined default], nor either of the string values localInputs.token / localInputs.name."

Thus, in order to solve the OP's original problem ...

"How should I get these stored items and return them?"

... the OP's provided approach might be best refactored into two functions, one which fulfills the getNameAndToken part and another one which covers the already existing hasNameAndToken part ...

// `storedInputs` structure ...
// {
//   "name":"John Doe",
//   "token":"string123-9882",
// }
export const getNameAndToken = () => {
  const storedInputs = localStorage.getItem('inputs');

  return (storedInputs !== null)
    && JSON.parse(storedInputs)
    || {};
};
export const hasNameAndToken = () => {
  const { name, token } = getNameAndToken();
  return !!(name && token);
};


// usage in other modules
import { getNameAndToken/*, hasNameAndToken*/ } from '/'

const { name, token } = getNameAndToken();
about 4 years ago · Juan Pablo Isaza Report

0

If you localInputs format is an object

{
   "name":"John Doe",
   "token":"string123-9882",
}

You should just take a property from this object, not from localInputs[0]:

const hasToken = localInputs.token;
const hasName = localInputs.name;
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!