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

347
Views
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 answers
Answer question

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 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!