react-redux icon indicating copy to clipboard operation
react-redux copied to clipboard

How do I use React redux without react router?

Open ccorcos opened this issue 11 years ago • 6 comments

How does this provider stuff work?

ccorcos avatar Oct 14 '15 00:10 ccorcos

Hi @ccorcos, have you seen this? Check out the Connecting to Redux section.

If that doesn't answer your question, could you be a little more specific?

ellbee avatar Oct 14 '15 12:10 ellbee

<Provider> puts store to be available on React context. It doesn't do anything else and isn't related to routing.

gaearon avatar Oct 14 '15 18:10 gaearon

Interesting. I've had to deal with piping tons of props from a top-level component all the way through. I think using the splats with props is easy enough though...

If my understanding is correct, then I can create my own provider something like this:

let Provider = React.createClass({
  componentWillMount: function() {
    this.unsubscribe = this.props.store.subscribe(() => {
      this.setState(this.props.store.getState())
    })
  },
  componentWillUnmount: function() {
    this.unsubscribe()
  },
  render: function() {
    let Component = this.props.component
    return (
      <Component {...this.state}/>
    )
  }
})

React.render(
  <Provider store={store} component={App}/>, 
  document.getElementById('root')
)

ccorcos avatar Oct 14 '15 18:10 ccorcos

I'm missing your point.

What you seem to be describing is what connect() does. <Provider> itself just puts store in context so connect() can read it regardless of how deeply nested the component is in view hierarchy. You can use connect() without <Provider> as long as you pass store down manually via props; <Provider> is what lets you avoid this manual threading.

What's your use case?

gaearon avatar Oct 14 '15 19:10 gaearon

Ah. I see. My use case is that I'm just trying to understand Redux. I want to know whats really going on before I use all these magical functions.

ccorcos avatar Oct 14 '15 22:10 ccorcos

I think I get it now though. Provider just makes store available via context. And connect does all the subscription stuff, piping store.getState() to props.

ccorcos avatar Oct 14 '15 22:10 ccorcos