【发布时间】:2020-05-29 02:02:21
【问题描述】:
我正在使用反应钩子,但其中 useEffect 不起作用。正如在控制台开始和结束不打印。我也在rails项目中使用react。
import PropTypes from 'prop-types';
import React, { useState, useEffect } from 'react';
import { Link } from 'react-router-dom';
const DetailInterview = () => {
const [participants, setparticipants] = useState([])
const [interviews, setinterviews] = useState([])
async function fetchParticipantsList() {
fetch('/api/v1/participants').
then((response) => response.json()).
then((participants) => setparticipants({ participants }));
};
async function fetchInterviewsList() {
const { match: { params: { id } } } = this.props;
fetch(`/api/v1/interviews/${id}`).
then((response) => response.json()).
then((interviews) => setinterviews({ interviews }));
console.log("sa",interviews)
};
useEffect(() => {
console.log('start')
fetchParticipantsList();
fetchInterviewsList();
console.log('end')
});
const interviewslist = JSON.stringify(interviews)
console.log('interviewlist: ',interviewslist)
return (
<div>
<h3>All participants</h3>
<table>
<thead>
<tr>
<th>ID</th>
<th>Title</th>
<th>Description</th>
<th>Is Published</th>
</tr>
</thead>
<tbody>
{
interviewslist.map((interview) => {
return (
<tr key={interview.id}>
<td>{interview.id}</td>
<td>{interview.interview_id}</td>
<td>
{/* <Link to={`/posts/${post.id}`}> */}
{interview.participant_id}
{/* </Link> */}
</td>
<td>{interview.created_at}</td>
<td>'Yes No'</td>
</tr>
)
})
}
</tbody>
</table>
</div>
);
}
export default DetailInterview;
任何人都可以帮助我为什么这不起作用。因此,我认为它正在显示 这个错误:
未捕获的类型错误:interviewslist.map 不是函数。
这个控制台也打印了两次:console.log('interviewlist: ',interviewslist)
【问题讨论】:
标签: reactjs react-hooks