diegohaz / redux-saga-thunk

Dispatching an action handled by redux-saga returns promise
MIT License
221 stars 17 forks source link

Ability to invalidate action state trough dispatching exported action #22

Closed braska closed 6 years ago

braska commented 6 years ago

What do you thing about possibility to implement exportable action (for example invalidate) that can be dispatched from user space to invalidate state of specific action?

For example:

import React from 'react';
import { invalidate, fulfilled } from 'redux-saga-thunk';
import { connect } from 'react-redux';

const MyComponent = ({ fulfilled, invalidate }) => (
  <div>
    <div>Is fulfilled: {fulfilled ? 'true' : 'false'}</div>
    {fulfilled && <button onClick={invalidate}>Invalidate</button>}
  </div>
);

export default connect(state => ({
  fulfilled: fulfilled(state, 'MY_CUSTOM_ACTION'),
}), { invalidate })(MyComponent); 

Clicking on button will trigger setting fulfilled to false.

diegohaz commented 6 years ago

Hi, @braska.

Could you tell me a use case for this?

Would invalidate clean only MY_CUSTOM_ACTION fulfilled state or all actions?

Would invalidate clean only fulfilled state or all states?

braska commented 6 years ago

Some words about use case:

I am using redux-saga-thunk to do SSR.

And I want to implement following behaviour: I have some page with info about company (/companies/:id). If user opens this page for the first time or in new tab (in other words, using SSR), I don't want to start API request to fetch company info, because required info already in state. For that I am using fulfilled state - I am fetch company info only in case if fulfilled = false. If page is server-side rendered then fulfilled = true on client-side (redux state transferred from server to client in window.__INITIAL_STATE__).

But what if user leaves this page (via client-side routing) and then turns back to it? Fetching doesn't start because fulfilled already equals true.

For resolve this issue I want to invalidate state of LOAD_COMPANY_INFO action on execution componentWillUnmount of CompanyPage component.

Would invalidate clean only MY_CUSTOM_ACTION fulfilled state or all actions? and Would invalidate clean only fulfilled state or all states?

All states, but only for MY_CUSTOM_ACTION.

diegohaz commented 6 years ago

I see.

I think clean('ACTION_TYPE') would be good.

Do you want to work on that?

braska commented 6 years ago

My current project depends on this feature. Currently I am using a bunch of workarounds and it is frustrating me. I will be glad to work on this, but don't have enough time to give any guarantees.

More over another issue (#20) is more important for me. And I want to start working on #20.

I would start when I have some free time.