Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Reactjs 如何在服务调用中设置我的阵列_Reactjs - Fatal编程技术网

Reactjs 如何在服务调用中设置我的阵列

Reactjs 如何在服务调用中设置我的阵列,reactjs,Reactjs,我有一个数组,我正在进行一个服务调用,我想将所有返回的json数组设置为我的react数组这里是到目前为止的代码: interface QuestionHeader { id: number; questionName: string; Question: Question; } const [questions, setQuestions] = useState<QuestionHeader[]>([]); function runService() {

我有一个数组,我正在进行一个服务调用,我想将所有返回的json数组设置为我的react数组这里是到目前为止的代码:

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()))
}