Company logo
  • Empleos
  • Bootcamp
  • Acerca de nosotros
  • Para profesionales
    • Inicio
    • Empleos
    • Cursos y retos
    • Preguntas
    • Profesores
    • Bootcamp
  • Para empresas
    • Inicio
    • Nuestro proceso
    • Planes
    • Pruebas
    • Nómina
    • Blog
    • Calculadora

0

627
Vistas
ReactJS: already declared in the upper scope

I have declared a variable like

const [listRealm, setListRealm] = useState([
    {
      value: null,
      label: 'select one',
    },
  ]);

  const findList= async () => {
    await axios.get(`${apiUrl}`).then(resp => {
      resp.data.map(res => {
        setListRealm(listRealm => [...listRealm, { value: res.id, label: res.name}]); // error here
      });
    });
  };


  useEffect(() => {
    findList();
  }, []);

Now my problem is that I receive error:

'listRealm' is already declared in the upper scope @typescript-eslint/no-shadow

I don't understand how I can change name of variable, considering that i'm setting this variable.

How can I do? thank you

7 months ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

The name you use in the setListRealm callback can be anything you want, so you can just make it previouslistRealm or something:

setListRealm(previousListRealm => [...previousListRealm, { value: res.id, label: res.name}]);

It doesn't have to match the name of the constant you've put the value from useState in.

7 months ago · Juan Pablo Isaza Denunciar

0

setListRealm(listRealm <-- You can name it here.

7 months 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 empleo Planes Nuestro proceso Comercial
Legal
Términos y condiciones Política de privacidad
© 2023 PeakU Inc. All Rights Reserved.