Reactjs 如何在服务调用中设置我的阵列
我有一个数组,我正在进行一个服务调用,我想将所有返回的json数组设置为我的react数组这里是到目前为止的代码:Reactjs 如何在服务调用中设置我的阵列,reactjs,Reactjs,我有一个数组,我正在进行一个服务调用,我想将所有返回的json数组设置为我的react数组这里是到目前为止的代码: interface QuestionHeader { id: number; questionName: string; Question: Question; } const [questions, setQuestions] = useState<QuestionHeader[]>([]); function runService() {
interface QuestionHeader {
id: number;
questionName: string;
Question: Question;
}
const [questions, setQuestions] = useState<QuestionHeader[]>([]);
function runService() {
const requestOptions = {
method: 'GET',
headers: { 'Content-Type': 'application/json' }
};
fetch(`URL`, requestOptions)
.then(res => res.json())
.then(e => {
setQuestions()
});
}
接口标题{
id:编号;
问题名称:字符串;
问题:问题;;
}
const[questions,setQuestions]=useState([]);
函数runService(){
常量请求选项={
方法:“GET”,
标题:{'Content-Type':'application/json'}
};
获取(`URL`,requestOptions)
.then(res=>res.json())
.然后(e=>{
设置问题()
});
}
以下是JSON:
{
“id”:2,
“问题ID”:2,
“问题名称”:“客户满意度”,
“问题”:[
{
“问题ID”:2,
“问题内容”:“品牌满意度”
}
]
}你可以这样做
const [questions, setQuestions] = useState<QuestionHeader[]>([]);
function runService() {
const requestOptions = {
method: 'GET',
headers: { 'Content-Type': 'application/json' }
};
fetch("URL", requestOptions)
.then(res => setQuestions(res.json()))
}
const[questions,setQuestions]=useState([]);
函数runService(){
常量请求选项={
方法:“GET”,
标题:{'Content-Type':'application/json'}
};
获取(“URL”,请求选项)
。然后(res=>setQuestions(res.json()))
}