【问题标题】:How can I get the data back with useState?如何使用 useState 取回数据?
【发布时间】:2021-06-27 15:38:09
【问题描述】:
  const noticeList = useSelector(state => state.noticeReducer.list) //현재 페이지에 띄워질 공지 리스트

  //page
  const [current, setCurrent] = useState(0);  //현재 페이지
  const pageInfo = useSelector(state => state.noticeReducer.pageInfo);  //전체 페이지 정보
  const [keyword, setkeyword] = useState(null);       //키워드 state
  const [searchedList, setsearchedList] = useState(noticeList);       // 검색 할때만 사용하므로 여기에 사용
  const [active, setactive] = useState("");
  console.log(searchedList)

  const Search = () => {
      const data = axios.post('/noticeList',{
          keyword : keyword,
      })
      .then(res => res.data)
      .catch(err => console.log(err));
  
      setsearchedList(data)
  }
useEffect(() => {
       return (
           dispatch(getNoticeList(current+1,keyword))  //공지사항 목록 받아오기
       )
   }, [dispatch, current])

//화면에 출력하기 위해 map 함수를 활용
let homeNotice = searchedList.map(
   item => 
  {

      return(
   <NoticeDetail key = {item.noticeId} title = {item.title} active = {active} setactive = {setactive} content = {item.content}/>
      )
      
   }
)

我在 Redux 中以 useEffect 的状态保存了数据。 我想在搜索功能中搜索时覆盖相同状态的数据。我该怎么办?

Uncaught TypeError: searchedList.map is not a function

【问题讨论】:

  • 你的“noticeReducer”中“list”的initialState是什么类型?

标签: reactjs api rest react-redux


【解决方案1】:

你没有正确使用承诺,你应该用承诺的结果来设置数据,而不是用承诺:

const Search = () => {
  axios
    .post('/noticeList', {
      keyword: keyword,
    })
    .then(({ data }) => setsearchedList(data))
    .catch((err) => console.log(err));
};

我假设 api 调用解析为 data 是一个数组。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-03-29
    • 1970-01-01
    • 1970-01-01
    • 2022-01-04
    • 1970-01-01
    • 2019-11-24
    • 2021-04-27
    • 1970-01-01
    相关资源
    最近更新 更多