React readOnlyError.js:2 Uncaught (in promise) TypeError: "searchData" is read-only

Viewed 273

As you can see in the code below that while typing or searching, the user also searches the data in the database for what the user is typing.

  const searchData = useState();
  const fetchSearchPerson = async (name) => {
    const { data } = await client.query({
      query: SEARCH_PERSON,
      variables: {
        name: name
      },
    })
     searchData = new Promise((resolve, reject) => {
      setTimeout(() => {
        resolve (data); 
      }, 1000);
    });
    return data;
  };

.....

  fetchSearchPerson(params.suggestTerm)
  return searchData.map((data) => {
    console.log("datas: ", data)
  });
 .....

Error I have encountered so far:

enter image description here

UPDATE

const [searchData, setSearchData] = useState();
  
  const fetchSearchPerson = async (name) => {
   
    const { data } = await client.query({
      
      query: SEARCH_PEOPLE,
      variables: {
        name: name,
      },
    })
    const searchDataPromise = new Promise((resolve, reject) => {
      setTimeout(() => {
        resolve (data); 
      }, 1000);
    });
    setSearchData(searchDataPromise); // add this
    return data;
  };

 fetchSearchPerson(params.suggestTerm)
 return searchData.map((data) => {
    console.log("datas: ", data)
  })

I dont received the error but the result from the search data

0 Answers
Related