React-rails: Is there a way to pass a context object to react_component

Created on 7 Dec 2017  路  7Comments  路  Source: reactjs/react-rails

Steps to reproduce

The current syntax for react_component doesnt let you specify context.

Expected behavior

To be able to pass an object that would act as the context for the rendered component.

Actual behavior

React component has no context when rendered.

discussion

Most helpful comment

@BookOfGreg Is it possible for react_component to take a render prop, this way i can use react_component to render a provider component and have it render this.props.render

_erb_

<%= react_component "Provider", {render: 'ProductPage', context: {foo: 'bar'}} %>

_Provider.js_

import Context from './context'
class Provider extends Component {
 render(
     return(
         <Context.Provider value={{...this.props.context}}>
          {this.props.render}
        </Context.Provider>
     )
)
}

_ProductPage.js_

import Context from './context'
class ProductPage extends Component {
 render(
     return(
         <Context.Consumer>
          {(context) => (
              <p>{context.foo}</p>
          )}
        </Context.Consumer>
     )
)
}

This way anything inside Provider should be able to access context without passing props around.

All 7 comments

Im having a similar problem, and we endup building a wrapContext function, that takes creates a higher order component and extracts context from props, it works but it feels hacky coz it seems like this is something that should be done by the framework.

What is the use case for passing in a context to the render_component helper?
Are you referring to the same context that is set by default here?: https://github.com/reactjs/react-rails/blob/master/lib/generators/react/install_generator.rb#L99

@3den I'd be interested in seeing your solution if possible so that I can understand more about the use case and look for possible ways to help :)

The react context is just meant to be an object such that all the child components in the tree can access.
Its usually acts like a global variable that is set at the top level component and is treated like a read-only global in the children without having to explicitly pass them down as a prop.
Popular libraries like react-router use it as well.

@BookOfGreg Is it possible for react_component to take a render prop, this way i can use react_component to render a provider component and have it render this.props.render

_erb_

<%= react_component "Provider", {render: 'ProductPage', context: {foo: 'bar'}} %>

_Provider.js_

import Context from './context'
class Provider extends Component {
 render(
     return(
         <Context.Provider value={{...this.props.context}}>
          {this.props.render}
        </Context.Provider>
     )
)
}

_ProductPage.js_

import Context from './context'
class ProductPage extends Component {
 render(
     return(
         <Context.Consumer>
          {(context) => (
              <p>{context.foo}</p>
          )}
        </Context.Consumer>
     )
)
}

This way anything inside Provider should be able to access context without passing props around.

@johnmailey Will this work right now?

@i2chris I can see it's being used in the wild so I'd say so.

I have moved away from react_rails and i cant remember why I put this here but looking back it looks like something that would work. @i2chris.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

hieuhlc picture hieuhlc  路  4Comments

Deekor picture Deekor  路  4Comments

ttanimichi picture ttanimichi  路  4Comments

axhamre picture axhamre  路  3Comments

adoseofjess picture adoseofjess  路  4Comments