【问题标题】:Redux saga - yield take not return actionRedux saga - yield 不返回动作
【发布时间】:2019-12-17 05:42:46
【问题描述】:

我在 github 上的 react-boilerplate 开源项目中使用 redux saga,并且我正在使用 redux saga 删除用户。该代码在 create-react-app 上完美运行,但不适用于 react-boilerplate (https://github.com/yarusophialiu/react-boilerplate/blob/master/app/containers/HomePage/saga.js)。问题是 yield take(...) 返回 null。我 console.log action.js 并发现当我单击删除时调度了一个动作。任何帮助将不胜感激!!!

主页/saga.js

export function* deleteUser({ userId }) {
  try {
    yield call(api.deleteUser, userId);
    // yield call(getUsers);
  } catch (e) {
    console.log(e);
  }
}

export function* watchDeleteUserRequest() {
  // yield takeLatest(DELETE_USER_REQUEST, getRepos);
  while (true) {
    const action = yield take(DELETE_USER_REQUEST);
    yield call(deleteUser, {
      userId: action.payload.userId,
    });
  }
}

主页操作.js

export const deleteUserRequest = userId => ({
  type: DELETE_USER_REQUEST,
  payload: {
    userId,
  },
});

主页/index.js

...
import { createStructuredSelector } from 'reselect';

import { useInjectReducer } from 'utils/injectReducer';
import { useInjectSaga } from 'utils/injectSaga';
import { changeUsername, deleteUserRequest } from './actions';
import reducer from './reducer';
import saga from './saga';
import UserList from '../UsersPage/UserList';

const key = 'home';

export function HomePage({
  username,
  loading,
  error,
  repos,
  onSubmitForm,
  onChangeUsername,
}) {
  useInjectReducer({ key, reducer });
  console.log('home', saga);
  useInjectSaga({ key, saga });

  const [users, setUsers] = useState([]);

  useEffect(() => {
    axios
      .get('https://rem-rest-api.herokuapp.com/api/users', {
        params: {
          limit: 1000,
        },
      })
      .then(({ data }) => {
        setUsers(data.data);
      });
  }, []);

  const handleDeleteUserClick = userId => {
    console.log('delete', deleteUserRequest(userId));
    deleteUserRequest(userId);
  };

  const reposListProps = {
    loading,
    error,
    repos,
  };

  return (
    <article>
      <div>
        <UserList users={users} onDeleteUser={handleDeleteUserClick} />
      </div>
    </article>
  );
}

...

【问题讨论】:

    标签: redux-saga


    【解决方案1】:

    尝试调度您的操作,以便您的 saga 知道它:

    import { useDispatch } from 'react-redux';
    
    export function HomePage({
      username,
      loading,
      error,
      repos,
      onSubmitForm,
      onChangeUsername,
    }) {
    
      const [users, setUsers] = useState([]);
      const dispatch = useDispatch(); // use to dispatch actions
    
      ...
    
      const handleDeleteUserClick = userId => {
        dispatch(deleteUserRequest(userId)); // Here you need to use dispatch
      };
    
      ...
    
      return (
        <article>
          <div>
            <UserList users={users} onDeleteUser={handleDeleteUserClick} />
          </div>
        </article>
      );
    }
    

    另外,你的 saga 应该监听 action 并更新 reducer:

    export function* deleteUser({ userId }) {
      try {
        yield call(api.deleteUser, userId);
        yield put({ type: 'DELETE_USER_SUCCESS', payload: { userId });
      } catch (e) {
        console.log(e);
      }
    }
    
    export function* watchDeleteUserRequest() {
      yield takeLatest(DELETE_USER_REQUEST, deleteUser);
    }
    

    那么你的 reducer 应该更新状态:

    const initialState = {
        users: []
    };
    
    const handlers = {
        ['DELETE_USER_SUCCESS']: (state, payload) => {
            const users = [...state.users.filter(user => user.id !== payload.id);
            return { ...state, users: { ...state.pending, invite: false } };
        }
    };
    

    这应该可以处理您的情况,让我知道它是否有效 欢迎来到堆栈溢出

    【讨论】:

    • 是的,它有效!非常感谢!我唯一感到困惑的是调度。我在使用const dispatch = useDispatch(); 时得到了 index.js?2433:54 Uncaught TypeError: Object(...) is not a function,所以我通过 mapDispatchToProps 进行了调度。我在网上查找并尝试了不同版本的 react 和 react-dom,例如 ^16.7.2、16.8.0、next 等,但没有运气。对此有任何想法:)?
    • @SSS - 很高兴它可以工作:),您是否正确导入了它? import { useDispatch } from 'react-redux';
    • @SSS - 这可能是因为 reat-redux 包已过时,请确保您使用的是 react-redux v7.1.0。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-06-03
    • 2020-02-22
    • 2017-08-19
    • 2021-02-01
    • 2017-06-14
    • 2018-12-23
    • 1970-01-01
    相关资源
    最近更新 更多