使用MediaQuery时,Radium-React报告“请将您的应用程序包装在StyleRoot组件中”

时间:2017-07-20 17:34:02

标签: javascript reactjs radium

这就是我的组件的样子

import React from "react";
import Radium from "radium";
import StyleRoot from "radium";

var styles = {

    base: {
        background: 'blue',
        border: 0,
        borderRadius: 4,
        color: 'white',
        padding: '1.5em',
        width: '15%',

        '@media (min-width: 320px)': {
            width: '100%'
        },

        ':hover': {
            backgroundColor: 'red'
        },

        ':focus': {
            backgroundColor: 'green'
        },

        ':active': {
            backgroundColor: 'yellow'
        },
    },

    block: {
        display: 'block',

        ':hover': {
            boxShadow: '0 3px 0 rgba(0,0,0,0.2)'
        }
    },
};

class LoginPage extends React.Component {
    render() {
        return (
            <StyleRoot>
                <button style={[
                    styles.base,
                    this.props.block && styles.block
                ]}>
                    {this.props.children}
                </button>
            </StyleRoot>
        );
    }
}

// https://github.com/FormidableLabs/radium/issues/816
// https://github.com/storybooks/storybook/issues/455
export default Radium(LoginPage);

我的package.json条目看起来像

"radium": "^0.19.1",

我甚至尝试使用以下导入

import {StyleRoot} from "radium";

但错误仍然相同。整个堆栈跟踪看起来像

ReactDOMComponentTree.js:113 Uncaught TypeError: Cannot read property '__reactInternalInstance$l955y3wpg6n' of null
    at Object.getClosestInstanceFromNode (ReactDOMComponentTree.js:113)
    at findParent (ReactEventListener.js:38)
    at handleTopLevelImpl (ReactEventListener.js:67)
    at ReactDefaultBatchingStrategyTransaction.perform (Transaction.js:143)
    at Object.batchedUpdates (ReactDefaultBatchingStrategy.js:62)
    at Object.batchedUpdates (ReactUpdates.js:97)
    at dispatchEvent (ReactEventListener.js:147)
getClosestInstanceFromNode @ ReactDOMComponentTree.js:113
findParent @ ReactEventListener.js:38
handleTopLevelImpl @ ReactEventListener.js:67
perform @ Transaction.js:143
batchedUpdates @ ReactDefaultBatchingStrategy.js:62
batchedUpdates @ ReactUpdates.js:97
dispatchEvent @ ReactEventListener.js:147
ReactDOMComponentTree.js:113 Uncaught TypeError: Cannot read property '__reactInternalInstance$l955y3wpg6n' of null
    at Object.getClosestInstanceFromNode (ReactDOMComponentTree.js:113)
    at findParent (ReactEventListener.js:38)
    at handleTopLevelImpl (ReactEventListener.js:67)
    at ReactDefaultBatchingStrategyTransaction.perform (Transaction.js:143)
    at Object.batchedUpdates (ReactDefaultBatchingStrategy.js:62)
    at Object.batchedUpdates (ReactUpdates.js:97)
    at dispatchEvent (ReactEventListener.js:147)
getClosestInstanceFromNode @ ReactDOMComponentTree.js:113
findParent @ ReactEventListener.js:38
handleTopLevelImpl @ ReactEventListener.js:67
perform @ Transaction.js:143
batchedUpdates @ ReactDefaultBatchingStrategy.js:62
batchedUpdates @ ReactUpdates.js:97
dispatchEvent @ ReactEventListener.js:147
ReactDOMComponentTree.js:113 Uncaught TypeError: Cannot read property '__reactInternalInstance$l955y3wpg6n' of null
    at Object.getClosestInstanceFromNode (ReactDOMComponentTree.js:113)
    at findParent (ReactEventListener.js:38)
    at handleTopLevelImpl (ReactEventListener.js:67)
    at ReactDefaultBatchingStrategyTransaction.perform (Transaction.js:143)
    at Object.batchedUpdates (ReactDefaultBatchingStrategy.js:62)
    at Object.batchedUpdates (ReactUpdates.js:97)
    at dispatchEvent (ReactEventListener.js:147)
getClosestInstanceFromNode @ ReactDOMComponentTree.js:113
findParent @ ReactEventListener.js:38
handleTopLevelImpl @ ReactEventListener.js:67
perform @ Transaction.js:143
batchedUpdates @ ReactDefaultBatchingStrategy.js:62
batchedUpdates @ ReactUpdates.js:97
dispatchEvent @ ReactEventListener.js:147
ReactDOMComponentTree.js:113 Uncaught TypeError: Cannot read property '__reactInternalInstance$l955y3wpg6n' of null
    at Object.getClosestInstanceFromNode (ReactDOMComponentTree.js:113)
    at findParent (ReactEventListener.js:38)
    at handleTopLevelImpl (ReactEventListener.js:67)
    at ReactDefaultBatchingStrategyTransaction.perform (Transaction.js:143)
    at Object.batchedUpdates (ReactDefaultBatchingStrategy.js:62)
    at Object.batchedUpdates (ReactUpdates.js:97)
    at dispatchEvent (ReactEventListener.js:147)
getClosestInstanceFromNode @ ReactDOMComponentTree.js:113
findParent @ ReactEventListener.js:38
handleTopLevelImpl @ ReactEventListener.js:67
perform @ Transaction.js:143
batchedUpdates @ ReactDefaultBatchingStrategy.js:62
batchedUpdates @ ReactUpdates.js:97
dispatchEvent @ ReactEventListener.js:147
ReactDOMComponentTree.js:113 Uncaught TypeError: Cannot read property '__reactInternalInstance$l955y3wpg6n' of null
    at Object.getClosestInstanceFromNode (ReactDOMComponentTree.js:113)
    at findParent (ReactEventListener.js:38)
    at handleTopLevelImpl (ReactEventListener.js:67)
    at ReactDefaultBatchingStrategyTransaction.perform (Transaction.js:143)
    at Object.batchedUpdates (ReactDefaultBatchingStrategy.js:62)
    at Object.batchedUpdates (ReactUpdates.js:97)
    at dispatchEvent (ReactEventListener.js:147)
getClosestInstanceFromNode @ ReactDOMComponentTree.js:113
findParent @ ReactEventListener.js:38
handleTopLevelImpl @ ReactEventListener.js:67
perform @ Transaction.js:143
batchedUpdates @ ReactDefaultBatchingStrategy.js:62
batchedUpdates @ ReactUpdates.js:97
dispatchEvent @ ReactEventListener.js:147
ReactDOMComponentTree.js:113 Uncaught TypeError: Cannot read property '__reactInternalInstance$l955y3wpg6n' of null
    at Object.getClosestInstanceFromNode (ReactDOMComponentTree.js:113)
    at findParent (ReactEventListener.js:38)
    at handleTopLevelImpl (ReactEventListener.js:67)
    at ReactDefaultBatchingStrategyTransaction.perform (Transaction.js:143)
    at Object.batchedUpdates (ReactDefaultBatchingStrategy.js:62)
    at Object.batchedUpdates (ReactUpdates.js:97)
    at dispatchEvent (ReactEventListener.js:147)
getClosestInstanceFromNode @ ReactDOMComponentTree.js:113
findParent @ ReactEventListener.js:38
handleTopLevelImpl @ ReactEventListener.js:67
perform @ Transaction.js:143
batchedUpdates @ ReactDefaultBatchingStrategy.js:62
batchedUpdates @ ReactUpdates.js:97
dispatchEvent @ ReactEventListener.js:147
ReactDOMComponentTree.js:113 Uncaught TypeError: Cannot read property '__reactInternalInstance$l955y3wpg6n' of null
    at Object.getClosestInstanceFromNode (ReactDOMComponentTree.js:113)
    at findParent (ReactEventListener.js:38)
    at handleTopLevelImpl (ReactEventListener.js:67)
    at ReactDefaultBatchingStrategyTransaction.perform (Transaction.js:143)
    at Object.batchedUpdates (ReactDefaultBatchingStrategy.js:62)
    at Object.batchedUpdates (ReactUpdates.js:97)
    at dispatchEvent (ReactEventListener.js:147)
getClosestInstanceFromNode @ ReactDOMComponentTree.js:113
findParent @ ReactEventListener.js:38
handleTopLevelImpl @ ReactEventListener.js:67
perform @ Transaction.js:143
batchedUpdates @ ReactDefaultBatchingStrategy.js:62
batchedUpdates @ ReactUpdates.js:97
dispatchEvent @ ReactEventListener.js:147
ReactDOMComponentTree.js:113 Uncaught TypeError: Cannot read property '__reactInternalInstance$l955y3wpg6n' of null
    at Object.getClosestInstanceFromNode (ReactDOMComponentTree.js:113)
    at findParent (ReactEventListener.js:38)
    at handleTopLevelImpl (ReactEventListener.js:67)
    at ReactDefaultBatchingStrategyTransaction.perform (Transaction.js:143)
    at Object.batchedUpdates (ReactDefaultBatchingStrategy.js:62)
    at Object.batchedUpdates (ReactUpdates.js:97)
    at dispatchEvent (ReactEventListener.js:147)
getClosestInstanceFromNode @ ReactDOMComponentTree.js:113
findParent @ ReactEventListener.js:38
handleTopLevelImpl @ ReactEventListener.js:67
perform @ Transaction.js:143
batchedUpdates @ ReactDefaultBatchingStrategy.js:62
batchedUpdates @ ReactUpdates.js:97
dispatchEvent @ ReactEventListener.js:147
ReactDOMComponentTree.js:113 Uncaught TypeError: Cannot read property '__reactInternalInstance$l955y3wpg6n' of null
    at Object.getClosestInstanceFromNode (ReactDOMComponentTree.js:113)
    at findParent (ReactEventListener.js:38)
    at handleTopLevelImpl (ReactEventListener.js:67)
    at ReactDefaultBatchingStrategyTransaction.perform (Transaction.js:143)
    at Object.batchedUpdates (ReactDefaultBatchingStrategy.js:62)
    at Object.batchedUpdates (ReactUpdates.js:97)
    at dispatchEvent (ReactEventListener.js:147)
getClosestInstanceFromNode @ ReactDOMComponentTree.js:113
findParent @ ReactEventListener.js:38
handleTopLevelImpl @ ReactEventListener.js:67
perform @ Transaction.js:143
batchedUpdates @ ReactDefaultBatchingStrategy.js:62
batchedUpdates @ ReactUpdates.js:97
dispatchEvent @ ReactEventListener.js:147
ReactDOMComponentTree.js:113 Uncaught TypeError: Cannot read property '__reactInternalInstance$l955y3wpg6n' of null
    at Object.getClosestInstanceFromNode (ReactDOMComponentTree.js:113)
    at findParent (ReactEventListener.js:38)
    at handleTopLevelImpl (ReactEventListener.js:67)
    at ReactDefaultBatchingStrategyTransaction.perform (Transaction.js:143)
    at Object.batchedUpdates (ReactDefaultBatchingStrategy.js:62)
    at Object.batchedUpdates (ReactUpdates.js:97)
    at dispatchEvent (ReactEventListener.js:147)
getClosestInstanceFromNode @ ReactDOMComponentTree.js:113
findParent @ ReactEventListener.js:38
handleTopLevelImpl @ ReactEventListener.js:67
perform @ Transaction.js:143
batchedUpdates @ ReactDefaultBatchingStrategy.js:62
batchedUpdates @ ReactUpdates.js:97
dispatchEvent @ ReactEventListener.js:147
ReactDOMComponentTree.js:113 Uncaught TypeError: Cannot read property '__reactInternalInstance$l955y3wpg6n' of null
    at Object.getClosestInstanceFromNode (ReactDOMComponentTree.js:113)
    at findParent (ReactEventListener.js:38)
    at handleTopLevelImpl (ReactEventListener.js:67)
    at ReactDefaultBatchingStrategyTransaction.perform (Transaction.js:143)
    at Object.batchedUpdates (ReactDefaultBatchingStrategy.js:62)
    at Object.batchedUpdates (ReactUpdates.js:97)
    at dispatchEvent (ReactEventListener.js:147)
getClosestInstanceFromNode @ ReactDOMComponentTree.js:113
findParent @ ReactEventListener.js:38
handleTopLevelImpl @ ReactEventListener.js:67
perform @ Transaction.js:143
batchedUpdates @ ReactDefaultBatchingStrategy.js:62
batchedUpdates @ ReactUpdates.js:97
dispatchEvent @ ReactEventListener.js:147
ReactDOMComponentTree.js:113 Uncaught TypeError: Cannot read property '__reactInternalInstance$l955y3wpg6n' of null
    at Object.getClosestInstanceFromNode (ReactDOMComponentTree.js:113)
    at findParent (ReactEventListener.js:38)
    at handleTopLevelImpl (ReactEventListener.js:67)
    at ReactDefaultBatchingStrategyTransaction.perform (Transaction.js:143)
    at Object.batchedUpdates (ReactDefaultBatchingStrategy.js:62)
    at Object.batchedUpdates (ReactUpdates.js:97)
    at dispatchEvent (ReactEventListener.js:147)
getClosestInstanceFromNode @ ReactDOMComponentTree.js:113
findParent @ ReactEventListener.js:38
handleTopLevelImpl @ ReactEventListener.js:67
perform @ Transaction.js:143
batchedUpdates @ ReactDefaultBatchingStrategy.js:62
batchedUpdates @ ReactUpdates.js:97
dispatchEvent @ ReactEventListener.js:147
ReactDOMComponentTree.js:113 Uncaught TypeError: Cannot read property '__reactInternalInstance$l955y3wpg6n' of null
    at Object.getClosestInstanceFromNode (ReactDOMComponentTree.js:113)
    at findParent (ReactEventListener.js:38)
    at handleTopLevelImpl (ReactEventListener.js:67)
    at ReactDefaultBatchingStrategyTransaction.perform (Transaction.js:143)
    at Object.batchedUpdates (ReactDefaultBatchingStrategy.js:62)
    at Object.batchedUpdates (ReactUpdates.js:97)
    at dispatchEvent (ReactEventListener.js:147)
getClosestInstanceFromNode @ ReactDOMComponentTree.js:113
findParent @ ReactEventListener.js:38
handleTopLevelImpl @ ReactEventListener.js:67
perform @ Transaction.js:143
batchedUpdates @ ReactDefaultBatchingStrategy.js:62
batchedUpdates @ ReactUpdates.js:97
dispatchEvent @ ReactEventListener.js:147
ReactDOMComponentTree.js:113 Uncaught TypeError: Cannot read property '__reactInternalInstance$l955y3wpg6n' of null
    at Object.getClosestInstanceFromNode (ReactDOMComponentTree.js:113)
    at findParent (ReactEventListener.js:38)
    at handleTopLevelImpl (ReactEventListener.js:67)
    at ReactDefaultBatchingStrategyTransaction.perform (Transaction.js:143)
    at Object.batchedUpdates (ReactDefaultBatchingStrategy.js:62)
    at Object.batchedUpdates (ReactUpdates.js:97)
    at dispatchEvent (ReactEventListener.js:147)
getClosestInstanceFromNode @ ReactDOMComponentTree.js:113
findParent @ ReactEventListener.js:38
handleTopLevelImpl @ ReactEventListener.js:67
perform @ Transaction.js:143
batchedUpdates @ ReactDefaultBatchingStrategy.js:62
batchedUpdates @ ReactUpdates.js:97
dispatchEvent @ ReactEventListener.js:147
ReactDOMComponentTree.js:113 Uncaught TypeError: Cannot read property '__reactInternalInstance$l955y3wpg6n' of null
    at Object.getClosestInstanceFromNode (ReactDOMComponentTree.js:113)
    at findParent (ReactEventListener.js:38)
    at handleTopLevelImpl (ReactEventListener.js:67)
    at ReactDefaultBatchingStrategyTransaction.perform (Transaction.js:143)
    at Object.batchedUpdates (ReactDefaultBatchingStrategy.js:62)
    at Object.batchedUpdates (ReactUpdates.js:97)
    at dispatchEvent (ReactEventListener.js:147)
getClosestInstanceFromNode @ ReactDOMComponentTree.js:113
findParent @ ReactEventListener.js:38
handleTopLevelImpl @ ReactEventListener.js:67
perform @ Transaction.js:143
batchedUpdates @ ReactDefaultBatchingStrategy.js:62
batchedUpdates @ ReactUpdates.js:97
dispatchEvent @ ReactEventListener.js:147
ReactDOMComponentTree.js:113 Uncaught TypeError: Cannot read property '__reactInternalInstance$l955y3wpg6n' of null
    at Object.getClosestInstanceFromNode (ReactDOMComponentTree.js:113)
    at findParent (ReactEventListener.js:38)
    at handleTopLevelImpl (ReactEventListener.js:67)
    at ReactDefaultBatchingStrategyTransaction.perform (Transaction.js:143)
    at Object.batchedUpdates (ReactDefaultBatchingStrategy.js:62)
    at Object.batchedUpdates (ReactUpdates.js:97)
    at dispatchEvent (ReactEventListener.js:147)
getClosestInstanceFromNode @ ReactDOMComponentTree.js:113
findParent @ ReactEventListener.js:38
handleTopLevelImpl @ ReactEventListener.js:67
perform @ Transaction.js:143
batchedUpdates @ ReactDefaultBatchingStrategy.js:62
batchedUpdates @ ReactUpdates.js:97
dispatchEvent @ ReactEventListener.js:147
ReactDOMComponentTree.js:113 Uncaught TypeError: Cannot read property '__reactInternalInstance$l955y3wpg6n' of null
    at Object.getClosestInstanceFromNode (ReactDOMComponentTree.js:113)
    at findParent (ReactEventListener.js:38)
    at handleTopLevelImpl (ReactEventListener.js:67)
    at ReactDefaultBatchingStrategyTransaction.perform (Transaction.js:143)
    at Object.batchedUpdates (ReactDefaultBatchingStrategy.js:62)
    at Object.batchedUpdates (ReactUpdates.js:97)
    at dispatchEvent (ReactEventListener.js:147)

我在遵循这些步骤时错过了什么?

1 个答案:

答案 0 :(得分:0)

它说&#34;包装你的应用程序&#34;因为在调用StyleRoot函数之前需要存在Radium

let Wrapper = ({ children }) => <StyleRoot>{children}</StyleRoot>

ReactDOM.render(
  <Wrapper><LoginPage /></Wrapper>,
  el,
)

您可能已经拥有一个不是您的LoginPage的根级别组件,因此您可以使用它而不是创建一个新的包装器组件。

import {StyleRoot} from 'radium';也是正确的语法。如果您要同时导入RadiumStyleroot,则会显示如下:

import Radium, {StyleRoot} from 'radium';