简体   繁体   English

React.js内联样式-如何从node_modules设置组件样式

[英]React.js inline styling - How to style Component from node_modules

I am currently using Draft.js in my React app. 我目前在我的React应用程序中使用Draft.js。 It is loaded via node_modules and then used in a Component, eg: 它通过node_modules加载,然后在Component中使用,例如:

import { Editor } from 'draft-js'

... ...

class MyComponent extends React.Component {
 render() {
    return(
        <div>
            <h1
                style={ styles.header }
            >Some header</h1>
            <div
                style={ styles.editorWrapper }
            >
                <Editor
                    style={ styles.editor }
                    placeholder="Write something!"
                />
            </div>

        </div>
    )
}
}

const styles = {
    editorWrapper: {
        backgroundColor: '#ececec',
        border: '2px solid #888888',
        padding: '25px'
    },
    editor: {
        backgroundColor: '#000'
    }
}

As described in the React docs, I can style my own Component (and divs etc) using inline styles, like the h1 or the editorWrapper div . 如React文档中所述,我可以使用内联样式(如h1或editorWrapper div )来设置自己的Component(以及div等)的样式。

Now trying to style the Editor(node_modules Component) the same way doesn't do anything. 现在尝试以相同方式设置Editor(node_modules Component)的样式不会执行任何操作。 I know that with my own Components I could pass the style as props and then use it in the child Component, but I really don't want to go through all the Draft.js code and change their Components. 我知道我可以使用自己的组件将样式作为道具传递,然后在子组件中使用它,但是我真的不想遍历所有Draft.js代码并更改其组件。

Is there a clean solution to style 3rd party Components? 是否有针对第3方组件样式的干净解决方案? Or do I have to use good old css. 还是我必须使用良好的旧CSS。 If so, isn't it bad to have some styling in the Components and other styling in external CSS? 如果是这样,那么在Components中进行某些样式设计并在外部CSS中进行其他样式设计是否不好?

draft-js has some hooks you can use to style parts of the Editor component, but generally there is no one way to style 3rd party components. draft-js具有一些可用于为Editor组件的各个部分设置样式的钩子 ,但是通常没有一种为3rd party组件设置样式的方法。 Many third party components will accept props passed in to override styles, material-ui does this quite well. 许多第三方组件将接受传递来覆盖样式的道具, material-ui做到这一点非常好。 If a third party component doesn't provide an option to pass in custom styles, you can wrap the component in a className and write css to override the default styles. 如果第三方组件不提供传递自定义样式的选项,则可以将该组件包装在className中,然后编写CSS来覆盖默认样式。 This might not work if the third party component is using inline styles, in which case you either have to use !important all over your css or fork the component entirely. 如果第三方组件使用内联样式,则此方法可能不起作用,在这种情况下,您必须在整个CSS中使用!important或完全对组件进行分叉。 Hope that helps! 希望有帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM