Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

185
Visualizações
How to remake and export/import function in React?

I need that useEffect in another component (not in a component directly connected to App.js). How can I remake and put it into a separate file? Will it better be a custom function to a custom hook? This is App.js component:

import React, {useState, useEffect} from 'react';
import { BrowserRouter as Router, Switch, Route } from "react-router-dom";
import axios from 'axios';
import Competitions from './components/Competitions';
import CompetitionForm from './components/CompetitionForm';
import Navigation from './components/Navigation';
import './styles/App.css'
 
const App = () => {
  const [competitions, setCompetitions] = useState([]);
  const [defaultCompetitionValue, setDefaultCompetitionValue] = useState({})

  useEffect(() => {
    axios.get('http://localhost:4000/app/getCompetitions')
      .then(res => res.data)
      .then(res => {
        for(let i = 0; i < res.length; i++) {
          competitions[i] = {
            "value": res[i].name,
            "label": res[i].name,
            "id": res[i]._id
          }
        };
        setCompetitions(competitions);
      })
      .then(() => setDefaultCompetitionValue({"value": competitions[0].value, "label": competitions[0].value}))
  }, [competitions]);

  if(competitions.length > 0 && defaultCompetitionValue) {
    return (
      <div className = "container">
        <Router>
          <div className = "sidebar">
            <Navigation />
          </div>
          <div className = "content">
            <Switch>
              <Route exact path = "/homepage">
                <Competitions
                  competitions = {competitions}
                  defaultCompetitionValue = {defaultCompetitionValue}
                />
              </Route>
              <Route exact path = "/CompetitionForm">
                <CompetitionForm competitions = {competitions}/>
              </Route>
            </Switch>
          </div>
        </Router>
      </div>
    )
  }
  return (<></>)
};

export default App;

I need that useEffect in another component (not directly connected to App.js). How can I remake and put it into a separate file? Will it better be a custom function to a custom hook?

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

useEffect is a hook which React provides for functional components. And you can use that in any component by importing from react as follows. You need not to pass from one component to another.

import { useEffect } from 'react';

For more details you can read their official docs here on hooks. https://reactjs.org/docs/hooks-reference.html

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda