Re-Calling a react hook after a function is executed

后端 未结 1 1869
醉话见心
醉话见心 2021-01-29 01:38

I am new to react hooks and I have a hook function which receives a series of data from an API to be shown in a list:

function useJobs () {
  const [jobs, setJob         


        
相关标签:
1条回答
  • 2021-01-29 02:04

    Your DeleteJobs function also needs to delete the job value from state as your data from server is only fetched on initial render. For that you can expose a method from custom hook and use it when calling DeleteJobs

    function useJobs () {
      const [jobs, setJobs] = React.useState([])
      const [locations, setLocations] = React.useState({})
      const [departments, setDepartments] = React.useState({})
      const [tags, setTags] = React.useState({})
    
      const deleteJob = (id) => { // function that removes job from state
          setJobs(prevJobs => prevJobs.filter(job => job.id !== id));
      }
      React.useEffect(() => {
        fetchJSON('/api/jobs/list-jobs', { headers: headers })
          .then(setJobs)
      }, [])
    
      ....
      return [jobs, locations, departments, tags,deleteJob]
    }
    
    
    function DeleteJob (jobid) {
      console.log('deletejob fired')
      console.log(jobid)
      axios({
        method: 'delete',
        url: '/api/jobs/delete-job/' + jobid,
        headers: headers
      })
    }
    
    export default function Jobs () {
      const classes = useStyles()
      const [jobs, locations, departments, tags, deleteJob] = useJobs()
      return (
          ...
          {jobs.map(job => (
          ......
           <IconButton 
              aria-label='delete'
              style={{ color: 'red' }} 
              variant='outlined' 
              onClick={() => { 
                  DeleteJob(job.id); 
                  deleteJob(job.id); // locally delete from state
              }}
           >
               <DeleteIcon />
           </IconButton>
    
          ...
      )
    }
    
    0 讨论(0)
提交回复
热议问题