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

220
Views
How can I extract my function into its own modules?

I want to extract some components and set them into their own modules. I want to set hook() and addName() into their own modules, but I don’t know how to call the variables/functions that they use.

So far I've tried to export my setState variables, but that doesn't work because I still need to use them within App().

I’ve tried passing setPersons.

import { useState, useEffect } from 'react'
import Persons from './components/persons'
import PersonForm from './components/personForm'
import Filter from './components/searchFilter'
import axios from 'axios'




const App = () => {
  const [persons, setPersons] = useState([])
  const [newName, setNewName] = useState('')
  const [newNumber, setNewNumber] = useState('')
  const [setFilter, setNewFilter] = useState('')
 

  const hook = () => {
  
    console.log("effect")
    axios 
      .get('http://localhost:3001/persons')
      .then(response =>{
        console.log('promise fulfilled')
        setPersons(response.data)
      })

  }
  useEffect (hook, []);
  
  const addName= () => {
    const nameObject =
    {
      name: newName,
      number: newNumber,
      id: persons.length + 1
    }

    axios 
      .post('http://localhost:3001/persons', nameObject)
      .then (response => {
        setPersons(persons.concat(response.data))
        
      })
  }

  const handleOnSubmit = (event) => {
    console.log(event)

    event.preventDefault();
    const find = persons.find(persons => persons.name === newName)
    if (find === undefined) addName()
    else {
      window.alert(newName + " has been added ")
      setNewName('')
      setNewNumber('')
    }
  }


 


  const handleOnChangeName = (event) => {
    setNewName(event.target.value)
  }
  const handleOnChangeNumber = (event) => {
    setNewNumber(event.target.value)
  }
  const handleOnChangeFilter = (event) => {
    setNewFilter(event.target.value)
  }



  return (

    <div>
      <div>
        <h2>Phonebook</h2>

        <Filter setFilter={setFilter} handleOnChangeFilter={handleOnChangeFilter} />

      </div>
      <h2>add a new</h2>
      <PersonForm
        newName={newName}
        newNumber={newNumber}
        handleOnSubmit={handleOnSubmit}
        handleOnChangeName={handleOnChangeName}
        handleOnChangeNumber={handleOnChangeNumber}
      />


      <h2>Numbers</h2>
      {
        <Persons person={persons} setFilter={setFilter} />
      }
    </div>

  )
}

 export default App
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

How about you make them accept parameters. First considering addName, it uses newName, newNumber, persons and setPersons. you can define it as

 const addName= (name, number, id, callback) => {
    const nameObject = { name, number, id }
    axios 
      .post('http://localhost:3001/persons', nameObject)
      .then (response => { callback(response.data) })
  }

and in your App.js you call is as

const handleOnSubmit = (event) => {
    console.log(event)

    event.preventDefault();
    const find = persons.find(persons => persons.name === newName)
    if (find === undefined) addName(newName, newNumber, persons.length + 1, (responsedata) => {  setPersons(persons.concat(responsedata) })
    else {
      window.alert(newName + " has been added ")
      setNewName('')
      setNewNumber('')
    }
  }

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!