React/Redux - How to call an action after component has rendered, based on a check - Getting errorHow to dispatch a Redux action with a timeout?What is mapDispatchToProps?How to dispatch Redux action from stateless component when route is loaded?Reducers state update not calling render function of parent component . React/ReduxReact, redux component does not update on route changeReact-Redux / Child component not re-renderingReactJS + Redux: Infinite render loop with (functional) Component wrapper + dispatch (state) in child ComponentReact Component render not called first time with reduxreact wait for component to get state from store then render childDispatching Redux actions on location change with react-router-dom

How to give my students a straightedge instead of a ruler

What the did the controller say during my approach to land (audio clip)?

Madrid to London w/ Expired 90/180 days stay as US citizen

Preventing the ears from getting clogged on descent to Barcelona

Can we have a C++ function with multiple return types? ( C++11 and above)

Delete empty subfolders, keep parent folder

Are there any instances in Tanach of Lashon Hara said purely for non-constructive purposes?

Do household ovens ventilate heat to the outdoors?

Why do things cool off?

Statistical tests for benchmark comparison

Why is belonging not transitive?

How is underwater propagation of sound possible?

How would you design and build an American Civil War Steam Gun?

SQL Server Always-On Availability Groups Patching

Social Encounters in a West Marches Campaign

Is Yang not precluded from conduting his "UBI experiment" as an electoral candidate?

What can I actually do with a high credit score?

Applications of mathematics in clinical setting

What is the word for a person who destroys monuments?

Secondary characters in character-study fiction

Who are the people reviewing far more papers than they're submitting for review?

Microservices and Stored Procedures

Why would a fighter use the afterburner and air brakes at the same time?

Why are two-stroke engines nearly unheard of in aviation?



React/Redux - How to call an action after component has rendered, based on a check - Getting error


How to dispatch a Redux action with a timeout?What is mapDispatchToProps?How to dispatch Redux action from stateless component when route is loaded?Reducers state update not calling render function of parent component . React/ReduxReact, redux component does not update on route changeReact-Redux / Child component not re-renderingReactJS + Redux: Infinite render loop with (functional) Component wrapper + dispatch (state) in child ComponentReact Component render not called first time with reduxreact wait for component to get state from store then render childDispatching Redux actions on location change with react-router-dom






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








1















I have, what should be a simple issue in which, when a user navigates to a specific route, the component fires an initial function call to grab a user by ID via a redux-observable in an epic. However, if the user navigates away from the page and then comes back, I need to be able to reload the page, based on a route parameter.



I have a component that utilizes an HOC to run the render() method, but it looks like a dumb component:



const ProfilePage = props => 
const actions, user, loading = props;

// Note: This if statement results in an error
if (user && user.id !== props.params.id)
actions.initAction(props.params.id);


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

ProfilePage.propTypes =
actions: PropTypes.object,
user: PropTypes.object,
loading: PropTypes.bool,
;


export default connect(
state => (
user: selectUser(state),
loading: selectLoading(state),
),
dispatch => ( actions: bindActionCreators(Actions, dispatch) )
)(
PureRender(
onMount(props => props.actions.initAction(props.params.id))(ProfilePage)
)
);


This results in an error:



react-dom.development.js?61bb:506 Warning: Cannot update during an existing state transition (such as withinrender). Render methods should be a pure function of props and state.



^ This happens because of my if statement that checks the user id against the params id.



Does this component need to be converted into a class in order to utilize other life cycle methods that could prevent this error from happening and run my functionality accordingly?










share|improve this question






























    1















    I have, what should be a simple issue in which, when a user navigates to a specific route, the component fires an initial function call to grab a user by ID via a redux-observable in an epic. However, if the user navigates away from the page and then comes back, I need to be able to reload the page, based on a route parameter.



    I have a component that utilizes an HOC to run the render() method, but it looks like a dumb component:



    const ProfilePage = props => 
    const actions, user, loading = props;

    // Note: This if statement results in an error
    if (user && user.id !== props.params.id)
    actions.initAction(props.params.id);


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

    ProfilePage.propTypes =
    actions: PropTypes.object,
    user: PropTypes.object,
    loading: PropTypes.bool,
    ;


    export default connect(
    state => (
    user: selectUser(state),
    loading: selectLoading(state),
    ),
    dispatch => ( actions: bindActionCreators(Actions, dispatch) )
    )(
    PureRender(
    onMount(props => props.actions.initAction(props.params.id))(ProfilePage)
    )
    );


    This results in an error:



    react-dom.development.js?61bb:506 Warning: Cannot update during an existing state transition (such as withinrender). Render methods should be a pure function of props and state.



    ^ This happens because of my if statement that checks the user id against the params id.



    Does this component need to be converted into a class in order to utilize other life cycle methods that could prevent this error from happening and run my functionality accordingly?










    share|improve this question


























      1












      1








      1








      I have, what should be a simple issue in which, when a user navigates to a specific route, the component fires an initial function call to grab a user by ID via a redux-observable in an epic. However, if the user navigates away from the page and then comes back, I need to be able to reload the page, based on a route parameter.



      I have a component that utilizes an HOC to run the render() method, but it looks like a dumb component:



      const ProfilePage = props => 
      const actions, user, loading = props;

      // Note: This if statement results in an error
      if (user && user.id !== props.params.id)
      actions.initAction(props.params.id);


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

      ProfilePage.propTypes =
      actions: PropTypes.object,
      user: PropTypes.object,
      loading: PropTypes.bool,
      ;


      export default connect(
      state => (
      user: selectUser(state),
      loading: selectLoading(state),
      ),
      dispatch => ( actions: bindActionCreators(Actions, dispatch) )
      )(
      PureRender(
      onMount(props => props.actions.initAction(props.params.id))(ProfilePage)
      )
      );


      This results in an error:



      react-dom.development.js?61bb:506 Warning: Cannot update during an existing state transition (such as withinrender). Render methods should be a pure function of props and state.



      ^ This happens because of my if statement that checks the user id against the params id.



      Does this component need to be converted into a class in order to utilize other life cycle methods that could prevent this error from happening and run my functionality accordingly?










      share|improve this question














      I have, what should be a simple issue in which, when a user navigates to a specific route, the component fires an initial function call to grab a user by ID via a redux-observable in an epic. However, if the user navigates away from the page and then comes back, I need to be able to reload the page, based on a route parameter.



      I have a component that utilizes an HOC to run the render() method, but it looks like a dumb component:



      const ProfilePage = props => 
      const actions, user, loading = props;

      // Note: This if statement results in an error
      if (user && user.id !== props.params.id)
      actions.initAction(props.params.id);


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

      ProfilePage.propTypes =
      actions: PropTypes.object,
      user: PropTypes.object,
      loading: PropTypes.bool,
      ;


      export default connect(
      state => (
      user: selectUser(state),
      loading: selectLoading(state),
      ),
      dispatch => ( actions: bindActionCreators(Actions, dispatch) )
      )(
      PureRender(
      onMount(props => props.actions.initAction(props.params.id))(ProfilePage)
      )
      );


      This results in an error:



      react-dom.development.js?61bb:506 Warning: Cannot update during an existing state transition (such as withinrender). Render methods should be a pure function of props and state.



      ^ This happens because of my if statement that checks the user id against the params id.



      Does this component need to be converted into a class in order to utilize other life cycle methods that could prevent this error from happening and run my functionality accordingly?







      reactjs redux react-redux redux-observable






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Mar 28 at 13:37









      Stevie StarStevie Star

      1,09517 silver badges34 bronze badges




      1,09517 silver badges34 bronze badges

























          2 Answers
          2






          active

          oldest

          votes


















          1
















          Function component should be pure, you can think of them as the "render" method of a class component.

          You can either use a class component and do side effects in componentDidMount / componentDidUpdate, or use hooks with useEffect.



          hooks useEffect / class cycle methods






          share|improve this answer
































            0
















            I am guessing the issue is with ProfilePage. Added error Cannot update during an existing state transition (such as withinrender). thrown mostly when a setState which is a async method, another setState gets called. For your case it's a functional component which you are using it in render method. So basically each time your component rerenders ProfilePage function gets called which calls the action. So you might wanna change ProfilePage this to a react component and use life cycle methods such as componentDidMount to fix your issue.
            Or if you are using react 16 or above use hooks with useEffect.






            share|improve this answer

























            • This did the trick with useEffect. Is there a downside to using this? Some of the other responses seem to say so

              – Stevie Star
              Mar 28 at 13:57











            • If I had to quote from react documentation, Yes! By default, it runs both after the first render and after every update. But you use it right, wouldn't be an issue. Read reactjs.org/docs/… also from a maintainable perspective, componentDidMount and other life cycles methos are very popular so oher developers are well aware of this. This is a bit new. So you might wanna think about the maintainability. Others no issues to use it if you are using it right.

              – Subhendu Kundu
              Mar 28 at 14:03














            Your Answer






            StackExchange.ifUsing("editor", function ()
            StackExchange.using("externalEditor", function ()
            StackExchange.using("snippets", function ()
            StackExchange.snippets.init();
            );
            );
            , "code-snippets");

            StackExchange.ready(function()
            var channelOptions =
            tags: "".split(" "),
            id: "1"
            ;
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function()
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled)
            StackExchange.using("snippets", function()
            createEditor();
            );

            else
            createEditor();

            );

            function createEditor()
            StackExchange.prepareEditor(
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader:
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/4.0/"u003ecc by-sa 4.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            ,
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            );



            );














            draft saved

            draft discarded
















            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55399009%2freact-redux-how-to-call-an-action-after-component-has-rendered-based-on-a-che%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown

























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            1
















            Function component should be pure, you can think of them as the "render" method of a class component.

            You can either use a class component and do side effects in componentDidMount / componentDidUpdate, or use hooks with useEffect.



            hooks useEffect / class cycle methods






            share|improve this answer





























              1
















              Function component should be pure, you can think of them as the "render" method of a class component.

              You can either use a class component and do side effects in componentDidMount / componentDidUpdate, or use hooks with useEffect.



              hooks useEffect / class cycle methods






              share|improve this answer



























                1














                1










                1









                Function component should be pure, you can think of them as the "render" method of a class component.

                You can either use a class component and do side effects in componentDidMount / componentDidUpdate, or use hooks with useEffect.



                hooks useEffect / class cycle methods






                share|improve this answer













                Function component should be pure, you can think of them as the "render" method of a class component.

                You can either use a class component and do side effects in componentDidMount / componentDidUpdate, or use hooks with useEffect.



                hooks useEffect / class cycle methods







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Mar 28 at 13:49









                Sagiv b.gSagiv b.g

                19.8k6 gold badges30 silver badges65 bronze badges




                19.8k6 gold badges30 silver badges65 bronze badges


























                    0
















                    I am guessing the issue is with ProfilePage. Added error Cannot update during an existing state transition (such as withinrender). thrown mostly when a setState which is a async method, another setState gets called. For your case it's a functional component which you are using it in render method. So basically each time your component rerenders ProfilePage function gets called which calls the action. So you might wanna change ProfilePage this to a react component and use life cycle methods such as componentDidMount to fix your issue.
                    Or if you are using react 16 or above use hooks with useEffect.






                    share|improve this answer

























                    • This did the trick with useEffect. Is there a downside to using this? Some of the other responses seem to say so

                      – Stevie Star
                      Mar 28 at 13:57











                    • If I had to quote from react documentation, Yes! By default, it runs both after the first render and after every update. But you use it right, wouldn't be an issue. Read reactjs.org/docs/… also from a maintainable perspective, componentDidMount and other life cycles methos are very popular so oher developers are well aware of this. This is a bit new. So you might wanna think about the maintainability. Others no issues to use it if you are using it right.

                      – Subhendu Kundu
                      Mar 28 at 14:03
















                    0
















                    I am guessing the issue is with ProfilePage. Added error Cannot update during an existing state transition (such as withinrender). thrown mostly when a setState which is a async method, another setState gets called. For your case it's a functional component which you are using it in render method. So basically each time your component rerenders ProfilePage function gets called which calls the action. So you might wanna change ProfilePage this to a react component and use life cycle methods such as componentDidMount to fix your issue.
                    Or if you are using react 16 or above use hooks with useEffect.






                    share|improve this answer

























                    • This did the trick with useEffect. Is there a downside to using this? Some of the other responses seem to say so

                      – Stevie Star
                      Mar 28 at 13:57











                    • If I had to quote from react documentation, Yes! By default, it runs both after the first render and after every update. But you use it right, wouldn't be an issue. Read reactjs.org/docs/… also from a maintainable perspective, componentDidMount and other life cycles methos are very popular so oher developers are well aware of this. This is a bit new. So you might wanna think about the maintainability. Others no issues to use it if you are using it right.

                      – Subhendu Kundu
                      Mar 28 at 14:03














                    0














                    0










                    0









                    I am guessing the issue is with ProfilePage. Added error Cannot update during an existing state transition (such as withinrender). thrown mostly when a setState which is a async method, another setState gets called. For your case it's a functional component which you are using it in render method. So basically each time your component rerenders ProfilePage function gets called which calls the action. So you might wanna change ProfilePage this to a react component and use life cycle methods such as componentDidMount to fix your issue.
                    Or if you are using react 16 or above use hooks with useEffect.






                    share|improve this answer













                    I am guessing the issue is with ProfilePage. Added error Cannot update during an existing state transition (such as withinrender). thrown mostly when a setState which is a async method, another setState gets called. For your case it's a functional component which you are using it in render method. So basically each time your component rerenders ProfilePage function gets called which calls the action. So you might wanna change ProfilePage this to a react component and use life cycle methods such as componentDidMount to fix your issue.
                    Or if you are using react 16 or above use hooks with useEffect.







                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Mar 28 at 13:52









                    Subhendu KunduSubhendu Kundu

                    1,6791 gold badge7 silver badges24 bronze badges




                    1,6791 gold badge7 silver badges24 bronze badges















                    • This did the trick with useEffect. Is there a downside to using this? Some of the other responses seem to say so

                      – Stevie Star
                      Mar 28 at 13:57











                    • If I had to quote from react documentation, Yes! By default, it runs both after the first render and after every update. But you use it right, wouldn't be an issue. Read reactjs.org/docs/… also from a maintainable perspective, componentDidMount and other life cycles methos are very popular so oher developers are well aware of this. This is a bit new. So you might wanna think about the maintainability. Others no issues to use it if you are using it right.

                      – Subhendu Kundu
                      Mar 28 at 14:03


















                    • This did the trick with useEffect. Is there a downside to using this? Some of the other responses seem to say so

                      – Stevie Star
                      Mar 28 at 13:57











                    • If I had to quote from react documentation, Yes! By default, it runs both after the first render and after every update. But you use it right, wouldn't be an issue. Read reactjs.org/docs/… also from a maintainable perspective, componentDidMount and other life cycles methos are very popular so oher developers are well aware of this. This is a bit new. So you might wanna think about the maintainability. Others no issues to use it if you are using it right.

                      – Subhendu Kundu
                      Mar 28 at 14:03

















                    This did the trick with useEffect. Is there a downside to using this? Some of the other responses seem to say so

                    – Stevie Star
                    Mar 28 at 13:57





                    This did the trick with useEffect. Is there a downside to using this? Some of the other responses seem to say so

                    – Stevie Star
                    Mar 28 at 13:57













                    If I had to quote from react documentation, Yes! By default, it runs both after the first render and after every update. But you use it right, wouldn't be an issue. Read reactjs.org/docs/… also from a maintainable perspective, componentDidMount and other life cycles methos are very popular so oher developers are well aware of this. This is a bit new. So you might wanna think about the maintainability. Others no issues to use it if you are using it right.

                    – Subhendu Kundu
                    Mar 28 at 14:03






                    If I had to quote from react documentation, Yes! By default, it runs both after the first render and after every update. But you use it right, wouldn't be an issue. Read reactjs.org/docs/… also from a maintainable perspective, componentDidMount and other life cycles methos are very popular so oher developers are well aware of this. This is a bit new. So you might wanna think about the maintainability. Others no issues to use it if you are using it right.

                    – Subhendu Kundu
                    Mar 28 at 14:03



















                    draft saved

                    draft discarded















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid


                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.

                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55399009%2freact-redux-how-to-call-an-action-after-component-has-rendered-based-on-a-che%23new-answer', 'question_page');

                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

                    SQL error code 1064 with creating Laravel foreign keysForeign key constraints: When to use ON UPDATE and ON DELETEDropping column with foreign key Laravel error: General error: 1025 Error on renameLaravel SQL Can't create tableLaravel Migration foreign key errorLaravel php artisan migrate:refresh giving a syntax errorSQLSTATE[42S01]: Base table or view already exists or Base table or view already exists: 1050 Tableerror in migrating laravel file to xampp serverSyntax error or access violation: 1064:syntax to use near 'unsigned not null, modelName varchar(191) not null, title varchar(191) not nLaravel cannot create new table field in mysqlLaravel 5.7:Last migration creates table but is not registered in the migration table

                    은진 송씨 목차 역사 본관 분파 인물 조선 왕실과의 인척 관계 집성촌 항렬자 인구 같이 보기 각주 둘러보기 메뉴은진 송씨세종실록 149권, 지리지 충청도 공주목 은진현