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

Initializing a redux store #277

Closed
stinoga opened this issue Jan 4, 2017 · 9 comments
Closed

Initializing a redux store #277

stinoga opened this issue Jan 4, 2017 · 9 comments
Labels

Comments

@stinoga
Copy link

stinoga commented Jan 4, 2017

Are there any good examples of initializing a redux store with styleguidist? Some of my components use a redux store, and I'd like to get one setup with dummy data for the style guide.

@aaronjensen
Copy link
Member

aaronjensen commented Jan 5, 2017

This is what we do (pardon the flow types):

WithState.js

// @flow
import React, { Component } from 'react'
import { Provider } from 'react-redux'
import { createStore } from 'redux'
import { reducer as reduxFormReducer } from 'redux-form'

import type { State } from 'app/reducers'

const reducer = ({ form, ...state }, action) => ({
  ...state,
  form: reduxFormReducer(form, action),
})

type Props = {
  state: $Shape<State> & {},
  children?: mixed,
}

class WithState extends Component {
  props: Props

  state: {
    store: mixed,
  }

  constructor(props: Props) {
    super()
    this.state = { store: createStore(reducer, props.state) }
  }

  render() {
    return (
      <Provider store={this.state.store}>
        {this.props.children}
      </Provider>
    )
  }
}

export default WithState

Header.examples.md

const WithState = require('scaffolds/WithState').default;
const state = {
  session: {
    currentUser: null,
  },
};

<WithState state={state}>
  <Header />
</WithState>

@sapegin
Copy link
Member

sapegin commented Jan 5, 2017

You can also redefine a Wrapper component and have store available for all your components. See example with IntlProvider: https://github.com/styleguidist/react-styleguidist/blob/master/docs/Cookbook.md#how-to-connect-redux-store

@stinoga
Copy link
Author

stinoga commented Jan 5, 2017

@sapegin that worked great thanks!

@stinoga stinoga closed this as completed Jan 5, 2017
@joepio
Copy link

joepio commented Jan 10, 2017

@stinoga Nice! Could you perhaps share how you created this wrapper that made the store available?

@sapegin
Copy link
Member

sapegin commented Jan 10, 2017

@stinoga Or even send a PR to the FAQ ;-)

@timurista
Copy link

timurista commented Apr 2, 2017

@drewandrew
Copy link

@timurista @sapegin https://github.com/styleguidist/react-styleguidist/blob/master/docs/Cookbook.md#how-to-connect-redux-store no longer exists. Anywhere else you would direct me/us for an example? I'm specifically looking to dispatch actions from a styleguidist README.md example.

@juandata
Copy link

juandata commented Apr 2, 2019

Inside your .md file import and dispatch like this:

import { Provider } from 'react-redux'
import * as actions from './redux/actions';
import store from './redux/store'
store.dispatch(actions.showConnection(true)); //your action 
(
<Provider store={store}>
  <App />
</Provider>
)

If you have issues with importing I suggest to change from npm to yarn since styleguidist has some issues with dependencies when installing with npm.

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

7 participants