markdalgleish / redial

Universal data fetching and route lifecycle management for React etc.
1.1k stars 42 forks source link

accessing store in provideHooks fetch #24

Closed shoebmogal closed 8 years ago

shoebmogal commented 8 years ago

Hello,

I need to access store in the fetch, so as to pass an access token while doing a request. How do I do that?

Thanks

markdalgleish commented 8 years ago

You can pass whatever you like into your hooks, including the entire store. Closing the issue for now, please re-open if you're having issues with this.

kkarkos commented 8 years ago

Hi @markdalgleish, I have a related question. In your example you are passing "params: {id}". I am using redux and would like to pass a state property, in my example "this.props.initinalPath" to the providerHook. But I am getting "this is not defined" because it looks like the store is not available yet. Above you are writing you could pass store data. Do you have an example how this would like?

My Code

function mapStateToProps (state) {
  return {
    initinalPath: state.sourceRequest.pathname
  }
}

@provideHooks({
  fetch: ({ dispatch, params: { this.props.initinalPath} }) => dispatch(loadContent(dataFromParams))
})@connect(mapStateToProps, null)
class Home extends React.Component {
kkarkos commented 8 years ago

Okay, I figured it out myself. Params get set on the server and passed to the component. I was confused not seeing the 'ID' in the server example.

server.js

const locals = {
 path: renderProps.location.pathname,
 query: renderProps.location.query,
 params: renderProps.params, 
// Allow lifecycle hooks to dispatch Redux actions:
 dispatch
 }
usopan commented 7 years ago

Can you post example component hook which refers these locals? I am unable to figure out a way to refer them in hook.

asicfr commented 7 years ago

You can pass whatever you like into your hooks, including the entire store.

Have you any example of that ? How can i pass store into defer or done function ? Thxs

PeterLittleDev commented 7 years ago

@usopan this worked for me fetch: ({ dispatch, path, params: { slug } }) => dispatch(getSomething(path))