简体   繁体   English

react js 使用 hooks 和 redux-saga 重复渲染组件

[英]react js using hooks and redux-saga rerender a component repeatedly

I'm trying to fetch data using redux-saga.我正在尝试使用 redux-saga 获取数据。 in result it renders my component over and over again every 2 or 3 seconds.结果,它每 2 或 3 秒一次又一次地渲染我的组件。 this is my component:这是我的组件:

const Farms = props => {

    useEffect(() => {
        props.listFarms();
    }, []);

    console.log(" [ FARMS ] let see that works :", props.farms);

    return (
        <div> </div>
    );
};

const mapStateToProps = state => {
    return {
        farms: state.farms
    }
};

const mapDispatchToProps ={ listFarms }

export default connect(mapStateToProps, mapDispatchToProps)(Farms);

and this is my saga generator:这是我的传奇生成器:

function* getAllFarms () {
    try {
        console.log(' [ farms saga ] all farms');
        const allFarms = yield call(getFarmsAsync);

        yield put(listFarms(allFarms.data))
    } catch (error) {
        console.log(' [ farms saga ] ERROR');
    }
}

this is my console output: ( it doesn't stop )这是我的控制台 output:(它不会停止)

在此处输入图像描述

anybody knows where is my mistake?有人知道我的错误在哪里吗?

From your code and the console output, it is clear that you are invoking getAllFarms worker saga again and again using yield put(listFarms(allFarms.data)) .从您的代码和控制台 output 可以清楚地看出,您正在使用yield put(listFarms(allFarms.data))一次又一次地调用getAllFarms工人传奇。

In other words, getAllFarms worker saga is listening to the action produced by listFarms , therefore it is being executed recursively.换句话说, getAllFarms worker saga 正在监听listFarms产生的动作,因此它是递归执行的。

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

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