Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Redux support needed #15

Open
BerndWessels opened this issue Mar 22, 2017 · 6 comments
Open

Redux support needed #15

BerndWessels opened this issue Mar 22, 2017 · 6 comments
Labels

Comments

@BerndWessels
Copy link

Hi
this is awesome project, but it definitely needs redux support.
Is this on the road-map?

@gabrielbull
Copy link
Owner

Do you have an example of how would you envision this to work?

@BerndWessels
Copy link
Author

Maybe something like this:

import {Provider} from 'preact-redux';
import {applyMiddleware, createStore} from 'redux';
import { renderToString } from 'react-router-server';
...

let store = createStore(
      rootReducer,
      applyMiddleware(epicMiddleware)
    );

renderToString(<Provider store={store}><App/></Provider>)
  .then(({ html, state }) => {
    // state here should actually be the redux-state at the end of all async operations.
  });

and just the normal redux connect within the components.

@pdiniz13
Copy link

pdiniz13 commented Apr 6, 2017

Would also be interested in knowing if this is in the works

@gabrielbull
Copy link
Owner

gabrielbull commented Apr 6, 2017

How would we know when all async operations are done in redux?

Wouldn't it currently work with redux by doing something like this:

import * as React from 'react';
import {fetchState} from 'react-router-server';
import {connect} from 'react-redux';
import {bindActionCreators} from 'redux'

@fetchState(
  state => state,
  actions => ({ done: actions.done })
)
@connect(
  ({ message }) => ({ message }),
  dispatch => ({ actions: bindActionCreators(actionCreators, dispatch) })
)
class Example extends React.PureComponent {
  componentWillMount() {
    this.props.actions.loadSomeMessage()
      .then((message) => this.props.done({ message }));
  }

 render() {
    return (
      <div>{this.props.message || 'Loading...'}</div>
    );
  }
}

export default Example;

@pdiniz13
Copy link

pdiniz13 commented Apr 7, 2017

what seems to be the most popular option is a double render on the server, the first one to create the state and the second to render the code.

    <Root store={store} history={history} />
  );


  // send signal to sagas that we're done
  store.dispatch(END);

  // wait for all tasks to finish
  await sagasDone();

  // capture the state after the first render
  const state = store.getState().toJS();
  const appState = `window.APP_STATE = ${htmlescape(state)}`;

  // 2nd render phase - the sagas triggered in the first phase are resolved by now
  const body = renderToString(
    <Root store={store} history={history} />
  );```

@oyeanuj
Copy link

oyeanuj commented Sep 25, 2017

@gabrielbull Could you clarify usage with Redux - use your comment above or the README 'with Redux` section?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants