简体   繁体   English

redux-saga takeEvery仅在您单击btn时调用,componentDidMount不会调用正确的操作

[英]redux-saga takeEvery only called when you click on btn, componentDidMount doesn`t call action correct

Sory for my English! 对不起,我的英语!

I am doing test work on React.js. 我正在对React.js进行测试。 The task is to make a regular blog. 任务是创建一个常规博客。 I ran into an unwanted problem. 我遇到了一个不必要的问题。 As a rule, componentDidMount makes entries, ready data and is called once. 通常,componentDidMount创建条目,准备好数据并被调用一次。 I invoke the loadPosts action in the CDM to get the data. 我在CDM中调用loadPosts操作以获取数据。 The takeEvery effect sees the necessary saga, but does not cause it, but skips it. takeEvery效果看到了必要的传奇,但并没有引起它,而是跳过了它。 When I press a button, everything works fine. 当我按下按钮时,一切正常。

I'm new to React. 我是React的新手。 All i tried is google 我只试过Google

repository with project branch - dev-fullapp 带有项目 branch - dev-fullapp 存储库 branch - dev-fullapp


index.js index.js

 import store from "./redux/store";

    const app = (
      <BrowserRouter>
        <Provider store={store}>
          <App />
        </Provider>
      </BrowserRouter>
    );

store.js store.js

    import { createStore, compose, applyMiddleware } from "redux";
    import createSagaMiddleware from "redux-saga";
    import apiSaga from "./sagas/index";
    import rootReducer from "./reducers/index";

    const initialiseSagaMiddleware = createSagaMiddleware();

    const storeEnhancers = window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__ || compose;

    const store = createStore(
      rootReducer,
      storeEnhancers(applyMiddleware(initialiseSagaMiddleware))
    );

    initialiseSagaMiddleware.run(apiSaga);
    export default store;

sagas.js sagas.js

    import { put, call, takeEvery } from "redux-saga/effects";
    import { fetchGetPosts } from "../apis/index";
    import { setErrorPosts, setPosts } from "../actions/actions-posts";

    function* workerGetPosts() {
        try {
            const posts = yield call(fetchGetPosts);
        yield put(setPosts(posts));
      } catch (err) {
            yield put(setErrorPosts(err));
      }
    }

    export default function* watchSaga() {
        yield takeEvery(POSTS.LOADING, workerGetPosts);
    }

actions.js actions.js

    import { POSTS } from "../constants";

    export const loadPosts = () => {
        console.log('action-load')
        return {
        type: POSTS.LOADING
        }
    };

    export const setPosts = payload => ({
      type: POSTS.LOAD_SUCCESS,
      payload
    });

    export const setErrorPosts = error => ({
      type: POSTS.ERROR,
      error
    });

rootReducer.js rootReducer.js

    import { combineReducers } from "redux";

    import postsReducer from "./reducer-posts";
    import loadReducer from "./reducer-load";

    const rootReducer = combineReducers({
      posts: postsReducer,
      isLoad: loadReducer
    });

    export default rootReducer;

reducer-posts.js 减速器posts.js

    import { POSTS } from "../constants";

    const postState = {
      posts: []
    };

    function postsReducer(state = postState, action) {
      switch (action.type) {
        case POSTS.LOAD_SUCCESS:
          return {
            ...state,
            posts: [...action.payload]
          };
        default:
          return state;
      }
    }

    export default postsReducer;

reducer-load.js 减速器load.js

    import { POSTS } from "../constants";
    import { combineReducers } from "redux";

    const loadReducerPosts = (state = false, action) => {
      switch (action.type) {
            case POSTS.LOADING: return false;
            case POSTS.LOAD_SUCCESS: return true;
        case POSTS.ERROR: return false;
        default: return state;
      }
    };

    const loadReducer = combineReducers({
      isLoadPost: loadReducerPosts,
    });

    export default loadReducer;

news.jsx news.jsx

    class News extends Component {
      componentDidMount() {
        loadPosts();
      }

      render() {
            CONST { loadPosts }this.props
        return (
          <main>

            // code

            <button onClick={loadPosts}>Test Button</button>
          </main>
        );
      }
    }

    const mapStateToProps = (
        { posts: { loading, posts, success } }
    ) => ({
      posts,
      loading,
      success
    });

    export default connect(
      mapStateToProps,
      { loadPosts }
    )(News);

在此处输入图片说明

loadPosts method is available as props to the React component in current case. 在当前情况下,loadPosts方法可用作React组件的道具。 Unlike in componentDidMount, on button click you are calling the function from props. 与componentDidMount中不同,在单击按钮时,您从props调用该函数。 You have to use this.props.loadPosts() on both places 您必须在两个地方都使用this.props.loadPosts()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM