material-ui icon indicating copy to clipboard operation
material-ui copied to clipboard

this.context.muiTheme is undefined

Open super-cache-money opened this issue 11 years ago • 35 comments

I've tried to use 0.8 with both React 13.3 and React 13.1.

I've tried a few components, including RaisedButton, and Checkbox.

As the title states, rendering breaks with the following error:

TypeError: Cannot read property 'component' of undefined

muiTheme is undefined.

I've triple checked my node_modules directory; there's only one version of react in there. There are no warnings, other than this old one:

Warning: bind(): React component methods may only be bound to the component instance. See exports

Inserting this:

contextTypes: {
    muiTheme: React.PropTypes.object.isRequired
}

gives the following warning:

Failed Context Types: Required context muiTheme was not specified in MyOtherwiseWorkingComponent. Check the render method of exports.

I'm so eager to try this out! What could be the issue here?

super-cache-money avatar May 25 '15 12:05 super-cache-money

Same here: #682

I can't make it work either :(

SebT avatar May 25 '15 13:05 SebT

See https://github.com/callemall/material-ui/blob/master/example/src/app/components/main.jsx#L5-L24, you have to add it manually now.

wmertens avatar May 25 '15 14:05 wmertens

I did add it. Then I had: Warning: owner-based and parent-based contexts differ (values:undefinedvs[object Object]) for key (muiTheme) while mounting TextField (see: http://fb.me/react-context-by-parent)

SebT avatar May 25 '15 14:05 SebT

@wmertens That really helped! I've added

childContextTypes: {
    muiTheme: React.PropTypes.object
},

getChildContext: function() {
    console.log('setting context!!');
    return {
        muiTheme: ThemeManager.getCurrentTheme()
    };
},

However, I'm still somehow getting the same error, when using, for example, RaisedButton. muiTheme is undefined.

Upon some investigation, I found that within render() of RaisedButton itself, this.context.muiTheme is indeed defined. However, within the subcomponent Paper's render(), this.context.muiTheme is undefined. Perhaps it's worth mentioning that the error originates from getStyles() within the Paper class. Furthermore, this happens only in the browserified bundle. It's fine rendering on the server. Weird!

What could be causing this?

super-cache-money avatar May 25 '15 21:05 super-cache-money

Figured it out. In the browser, I had two versions of react running.

This is probably not relevant for most people, but here's more info:

I'm using browserify and browserify-shim to bundle scripts. The shim transform wasn't set to global, so when a package in node_modules required react (like material-ui, in this case), the shim isn't applied and the whole library is inlined.

super-cache-money avatar May 26 '15 10:05 super-cache-money

@sebt I'm seeing the same, I have a component that simply adds this.props.children to its createElement call and it looks like they lose their context then. No time to investigate right now

wmertens avatar May 26 '15 17:05 wmertens

@SebT: context seems to suck: https://github.com/acdlite/flummox/issues/70#issuecomment-78110720 I also get that warning and my FlatButton doesn't render.

@super-cache-money https://github.com/facebook/react/issues/1939 strikes again

wmertens avatar May 27 '15 07:05 wmertens

@SebT I solved it for me with this non-perfect solution (CoffeeScript): Instead of using @props.children I use @props.children.map (c, i) -> React.addons.cloneWithProps c, key: i.

There are several things wrong with that, it should really use the ReactChildren iteration and it shouldn't replace the key if one is there, but in my case that doesn't matter and probably won't until 0.14 comes out.

wmertens avatar May 27 '15 07:05 wmertens

Well I tried again yesterday and this time it did work with childContext.

I don't really know what happened, I re-ran a npm install for another reason and maybe I had another version of React in my node_modules.

Anyway, thanks for the help @wmertens ;)

SebT avatar May 27 '15 08:05 SebT

@super-cache-money - I might have a similar issue which I am unable to solve. I am using webpack to convert the material-ui library and use it in the browser. I think that inlines the react library in that file itself.

Now I also have an external reference to a react library from my main html file

Is this why this conflict is happening ? Any way to work around it ?

agnivade avatar Aug 07 '15 10:08 agnivade

I will post how I solved this problem: I was including 'react/addons' in my vendors bundle file using browserify. However in my components I was requiring react in this way:

import React from 'react'; (not 'react/addons')

Once I changed 'react/addons' to 'react' in my vendors bundle file, error dissapeared.

p4004 avatar Aug 13 '15 07:08 p4004

I still can't make this to work. I use [email protected] and my router is like

<Router history={history}>
    <Route path="/" component={App}>
        <Route path="somePage" component={somePage} />
    </Route>
</Router>

I've already define getChildContext() in App component

export default class App extends React.Component {
    static childContextTypes = {
        muiTheme: PropTypes.object
    }
    getChildContext() {
        return {
            muiTheme: ThemeManager.getCurrentTheme()
        }
    }
    render() {
        return (
            <div>
                {this.props.children}
            </div>
        );
    }
}

in somePage component I use some material UI components, and I get this warning

"Warning: owner-based and parent-based contexts differ (values: `undefined` vs `[object Object]`) for key (muiTheme) while mounting TextField (see: http://fb.me/react-context-by-parent)" 

and this error

Uncaught TypeError: Cannot read property 'component' of undefined

it was because this.context.muiTheme is undefined. What else do I have to do?

Qrysto avatar Aug 23 '15 06:08 Qrysto

Use React 0.14 beta or clone the children.

On Sun, Aug 23, 2015, 08:14 Qrysto [email protected] wrote:

I still can't make this to work. I use [email protected] and my router is like

I've already define getChildContext() in App component

export default class App extends React.Component { static childContextTypes = { muiTheme: PropTypes.object } getChildContext() { return { muiTheme: ThemeManager.getCurrentTheme() } } render() { return (

{this.props.children}
); } }

in somePage.jsx I use some material UI components, and I get this warning

"Warning: owner-based and parent-based contexts differ (values: undefined vs [object Object]) for key (muiTheme) while mounting TextField (see: http://fb.me/react-context-by-parent)"

and this error

Uncaught TypeError: Cannot read property 'component' of undefined

it was because this.context.muiTheme is undefined. What else should I have to do?

— Reply to this email directly or view it on GitHub https://github.com/callemall/material-ui/issues/686#issuecomment-133792212 .

Wout. (typed on mobile, excuse terseness)

wmertens avatar Aug 23 '15 06:08 wmertens

now the problem is

peerinvalid Peer [email protected] wants react@~0.13.x || ~0.12.x

when i try to install [email protected] :laughing: . Btw what do you mean by "clone the children"?

Qrysto avatar Aug 23 '15 09:08 Qrysto

you can drop the tap-event plugin in react 0.14 I think :)

when you clone the children, they get a new owner (you) and the context works. See https://github.com/callemall/material-ui/issues/686#issuecomment-105807652

wmertens avatar Aug 23 '15 09:08 wmertens

Oh, the plugin is needed until React 1.0: https://github.com/zilverline/react-tap-event-plugin

wmertens avatar Aug 23 '15 09:08 wmertens

@wmertens thanks for the help. What exactly do you mean by cloning the children? @Qrysto I have this problem when I try to upgrade to react 0.14 beta. How are you solving it?

npm ERR! Darwin 13.4.0
npm ERR! argv "node" "/usr/local/bin/npm" "install"
npm ERR! node v0.10.33
npm ERR! npm  v2.1.7
npm ERR! code EPEERINVALID

npm ERR! peerinvalid The package react does not satisfy its siblings' peerDependencies requirements!
npm ERR! peerinvalid Peer [email protected] wants [email protected]
npm ERR! peerinvalid Peer [email protected] wants [email protected]
npm ERR! peerinvalid Peer [email protected] wants react@~0.13.x || ~0.12.x
npm ERR! peerinvalid Peer [email protected] wants react@>=0.13

npm ERR! Please include the following file with any support request:

sathify avatar Aug 24 '15 03:08 sathify

@sathify actually I haven't solved it yet :laughing: Probably I'll ready more about that "cloning the children" when I have time, but I wonder how people can still use MUI at the moment :question:

Qrysto avatar Aug 24 '15 17:08 Qrysto

Having the same problem with [email protected], right now don't know if that is the cause of the problem

soda29 avatar Aug 27 '15 01:08 soda29

I upgraded to [email protected] and the problem went away for me. Don't need react-tap-event-plugin (gives an error) and had to edit the node_modules/material-ui/package.json to remove the peerDependency. Messy workaround for that, but works.

@sathify YMMV with those fluxible peerDependencies.

nickspacek avatar Sep 03 '15 12:09 nickspacek