export const HomePage:React.FC=()=>{
const loading=useSelector(state=>state.recommendProduct.loading)
const productList =useSelector(state=>state.recommendProduct.productList)
const dispatch=useDispatch()
useEffect(()=>{
try {
dispatch(fetchRecommendProductStartActionCreator())
const feacthdate=async()=>{
const {data}=await axios.get(“http://123.56.149.216:8080/api/productCollections",{headers:{“x-icode”:"472DA4444926CAB2”}})
dispatch(fetchRecommendProductSuccessActionCreator(data))
}
feacthdate()
} catch (e) {
dispatch(fetchRecommendProductFailActionCreator(e.message))
}
},[] )
console.log(“productList”,productList)
console.log(“productList”,productList[0].touristRoutes)
老师用useSelector获取store数据后 console.log(“productList”,productList) 是可以取到数据的,但是访问数组下的某个字段是报TypeError: Cannot read property ‘touristRoutes’ of undefined的错误,我需要在哪里去定义productList的类型呢?
React18 精讲 + 结合 TS 实战 + 热门业务开发,获取必备技能
了解课程