복붙노트

[REACTJS] 함수가 실행 된 후 후크 반응 재 - 호출

REACTJS

함수가 실행 된 후 후크 반응 재 - 호출

해결법


  1. 1.귀하의 DeleteJobs는 서버에서 데이터로 상태에서 작업 값을 삭제해야하는 초기 렌더링 만 페치 기능을 수행합니다. DeleteJobs를 호출 할 때 사용자 정의 후크에서 방법을 노출하고 사용할 수있는 내용은

    귀하의 DeleteJobs는 서버에서 데이터로 상태에서 작업 값을 삭제해야하는 초기 렌더링 만 페치 기능을 수행합니다. 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>
    
          ...
      )
    }
    
  2. from https://stackoverflow.com/questions/62015830/re-calling-a-react-hook-after-a-function-is-executed by cc-by-sa and MIT license