Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

88
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda