admin管理员组文章数量:1414621
The React ponent is great because it lets us define our own tags, which can be more powerful than the original ones.
Currently I am only using the React ponent to define the HTML structure, and setting the className of each tag to let the style sheet modify its appearance. Is there a way to give a default style to ponents without using inline CSS?
One idea I have is to add one CSS file associated with one ponent, or add a tag directly in the jsx file for each ponent, then pile the CSS or jsx to put all the CSS in one file and added it in part.
The React ponent is great because it lets us define our own tags, which can be more powerful than the original ones.
Currently I am only using the React ponent to define the HTML structure, and setting the className of each tag to let the style sheet modify its appearance. Is there a way to give a default style to ponents without using inline CSS?
One idea I have is to add one CSS file associated with one ponent, or add a tag directly in the jsx file for each ponent, then pile the CSS or jsx to put all the CSS in one file and added it in part.
Share Improve this question edited Apr 21, 2015 at 22:49 Michael Laszlo 12.2k2 gold badges32 silver badges47 bronze badges asked Apr 20, 2015 at 6:24 ChengluChenglu 1,9972 gold badges15 silver badges26 bronze badges 3-
I do not think it is a clear solution to that atm. BUT at react conf in january they talked about their new "inline"-styling scheme which came with React-Native. With that you can create javascript objects which holds the styles.
StyleSheet.create({ 'myComponent' : { margin: 5, padding: 5 }});
This lets you keep the ponent CSS apart and you could then add the css to your ponent structure. – magnudae Commented Apr 20, 2015 at 7:22 - Is this the inline style? I think it's better to let the user have the privilege to modified the style of the ponent via the style sheet. This will let us build ponents with beautiful UI as well as the Scalability. – Chenglu Commented Apr 20, 2015 at 9:46
- Its inline style, but you separate it in a .js file instead of .css. This means it almost has the same purpose as a stylesheet. The user would only need to alter one file. Here is an example from a React-Native project im working on: jsfiddle/0aqgxr5v PS: it works with React as well. – magnudae Commented Apr 20, 2015 at 10:04
1 Answer
Reset to default 6It is easily expressed with inline style approach in React:
var pStyle = {
color: 'white',
backgroundImage: 'url(' + imgUrl + ')',
margin: 5
};
React.render(<div style={pStyle}>Component</div>, mountNode);
本文标签: javascriptHow to set the default style to a React ComponentStack Overflow
版权声明:本文标题:javascript - How to set the default style to a React Component - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745191008a2646909.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论