我正在try 学习AXIOS异步方法的Reaction-Redux的用法,我使用useEffect钩子调用异步函数并调度操作来存储数据并将数据分配给状态,但运行后无法获得状态.我确实从API获得了数据,只是没有分配给channel状态,也没有呈现.

import { configureStore } from '@reduxjs/toolkit';
import counterReducer from './modules/counterStore';
import channelReducer from './modules/channelStore';

// create store with reducer
const store = configureStore({
  reducer: {
    channel: channelReducer,
  },
});

export default store;
import { createSlice } from "@reduxjs/toolkit";
import axios from "axios";

const channelStore = createSlice({
  name: "channelStore",
  initialState: {
    channel: [],
  },
  reducers: {
    setChannel: (state, action) => {
      state.channel = action.payload;
    },
  },
});

const { setChannel } = channelStore.actions;
const channelReducer = channelStore.reducer;

const fetchChannelList = () => {
  return async (dispatch) => {
    await axios.get("http://geek.itheima.net/v1_0/channels").then(
      (res) => {
        dispatch(setChannel(res.data.data.channels))
        console.log(res.data.data.channels)
      }
    );
  };
}

export { fetchChannelList };
export default channelReducer;
import './App.css';
import { useSelector, useDispatch } from 'react-redux';
import { useEffect } from 'react';
import { fetchChannelList } from './store/modules/channelStore';

function App() {
  const { channelList } = useSelector(state => state.channel)
  const dispatch = useDispatch()

  useEffect(() => {
    dispatch(fetchChannelList())
  }, [dispatch])

  return (
    <div >
      <ul>
        {channelList?.map(item => {
          return (<li key={item.id}>{item.name}</li>)
        })}
      </ul>
    </div>
  );
}

export default App;

enter image description here

我试图从API获取数据,并使用分派操作将数据分配给状态并呈现在页面中.

推荐答案

Probable Issue

您正在设置channel状态,但UI试图从选定的state.channel中分离出一些channelList属性,这是一个只具有channel属性的数组对象.

const channelStore = createSlice({
  name: "channelStore",
  initialState: {
    channel: [], // <-- array
  },
  reducers: {
    setChannel: (state, action) => {
      state.channel = action.payload; // <-- array
    },
  },
});

const fetchChannelList = () => {
  return async (dispatch) => {
    await axios.get("http://geek.itheima.net/v1_0/channels").then(
      (res) => {
        dispatch(setChannel(res.data.data.channels)) // <-- array
        console.log(res.data.data.channels)
      }
    );
  };
}
const {
  channelList // <-- undefined property
} = useSelector(state => state.channel) // <-- channel state, e.g. { channel }

...

{channelList?.map(item => {
  return (
    <li key={item.id}>{item.name}</li>
  )
})}

错误shouldn't发生在您提供的代码示例中,因为您正在对channelList使用Optional Chaining运算符,但在实际代码中可能并非如此.无论哪种方式,channelList对于您所维护的状态都是不正确的.

Solution

Select 正确的州.state.channel.channel是它维护的channelStore切片的array.

const channel = useSelector(state => state.channel.channel);

...

{channel.map(item => (
  <li key={item.id}>{item.name}</li>
))}

Javascript相关问答推荐

Flisk和JS错误:未捕获的Syntax错误:意外的令牌'<'

具有相同参数的JS类

如何才能拥有在jQuery终端中执行命令的链接?

如何使用JavaScript将文本插入空div

配置WebAssembly/Emscripten本地生成问题

JS,当你点击卡片下方的绿色空间,而它是在它的背后转动时,

Prisma具有至少一个值的多对多关系

TypeError:无法读取未定义的属性(正在读取';宽度';)

如何 for each 输入动态设置输入变更值

Vaadin定制组件-保持对javascrip变量的访问

Reaction-SWR-无更新组件

顶点图使用组标签更新列之间的条宽

Pevent触发material 用户界面数据网格中的自动保存

P5.js中矩形内的圆弧

是否设置以JavaScript为背景的画布元素?

在HTML5画布上下文中使用putImageData时,重载解析失败

在ChartJS中使用spanGaps时,是否获取空值的坐标?

如何在Firebase中读取对象的特定字段?

当达到高度限制时,如何裁剪图像?使用html和css

我想为我的Reaction项目在画布上加载图像/视频,图像正在工作,但视频没有