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

338
Visualizações
Problem while building react-router-dom and passing hooks (onSubmit)

I have this kind of Router component:

import addProduct from "../components/Products"
[...]

 <BrowserRouter>
            <div className="content">
                <Switch>
                    <Route path="/products">
                        <Products />

                    </Route>
                    <Route path="/add">
                        <AddProduct onSubmit={addProduct} />
                        <Products />
                    </Route>
                </Switch>
            </div>
        </BrowserRouter>

Products, which look like this (I only paste the most important things):

 const addProduct = async (newProduct) => {
        const response = await axios.post('https://fakestoreapi.com/products', newProduct);
        console.log(response)
        setProducts([...products,response.data])
    }

[...]

inside return I have a link to form, where we can create a new product:


 <NavLink to="/add">
            <h2>Click me to add a new product</h2>
            </NavLink>


The problem is that before I started making react-router-dom, my component <AddProduct /> was inside the Products and that I could pass the addProduct function to AddProduct immedietaly, so I was making <AddProduct onSubmit={addProduct}/>. At the moment I don't know what I should do to pass my addProduct function to <AddProduct> component when it's not inside Products now.

AddProduct looks like this if it matters in this case:

import ProductsForm from './ProductsForm'
function AddProduct({onSubmit}) {
    return (
        <ProductsForm onSubmit={onSubmit} initialValues={{title: '', price: '', category: '', image: ''}} />
    )
}

export default AddProduct;
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

It looks like you may benefit from a state management system. There are a few around, including redux & mobx. You could also 'roll your own' using react's in-built Context API. This allows you to store your state in one location, and have only the components that need it subscribe to the store for data or functions to modify that data.

If this is only for a small app, you can get away with using Context (plus it's a great tool in your belt!). It's not so good for larger apps as it has only been designed to manage small kinds of data

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