【发布时间】:2021-09-10 00:22:05
【问题描述】:
我在一个名为 AllJobs 的组件中有以下代码
<TableCell>
<Link
to={`/job/${item.id}`}
style={{ textDecoration: 'underline', color: 'black' }}
>
{item.id}
</Link>
</TableCell>
当用户点击此链接时,它会在我的 App.js 文件中按照以下路由调用组件 JobDetails:
<Router>
<Switch>
<Route exact path="/job/:id">
<JobDetails />
</Route>
</Switch>
</Router>
我的问题是,在我的 JobDetails 组件中,我有一个未触发的 useEffect 并且不确定为什么它不是我需要它来调用我的 getJob() 函数?
export default function JobDetails() {
const { id } = useParams();
const [currentJob, setCurrentJob] = useState([]);
const getJob = async () => {
console.log("Inside JobDetails for id:", id)
try {
const response = await fetch(`http://localhost:3000/get-job/${id}`);
const currentJobRec = await response.json();
console.log("current-job", currentJobRec)
setCurrentJob(currentJobRec);
} catch (err) {
console.error(err.message);
}
};
useEffect(() => {
getJob();
}, []);
由于我需要触发 useEffect(),任何人都可以帮助我解决我所缺少的内容。
【问题讨论】:
标签: javascript reactjs react-router react-hooks