简体   繁体   English

创建React.js组件时如何设置样式?

[英]How to set the style a react.js component when creating it?

How to set the style a react.js component when creating it? 创建React.js组件时如何设置样式?

Below is some of my code (partially inherited from a stronger developer and then simplified for brevity). 以下是我的一些代码(部分继承自功能更强大的开发人员,为简洁起见对其进行了简化)。

I want to re-use my LogComponent to print several pages of a Log. 我想重新使用LogComponent来打印日志的几页。 However, in some cases I want to force a particular width on the returned List, rather than allowing it to flex as it sees fit. 但是,在某些情况下,我想在返回的List上强制使用特定的宽度,而不是允许它按其认为合适的方式弯曲。

I would prefer to not define a separate LogComponentFixed or to have an if (...) {return (...)} else {return(...)} in my LogComponent. 我宁愿不定义单独的LogComponentFixed,也不希望在LogComponent中使用if (...) {return (...)} else {return(...)}

I have in mind to do something in Log.js like: 我想在Log.js中做一些事情,例如:

<LogComponent heading={"Page 1"}, lines={page_1}, style={styles.list_1} />
<LogComponent heading={"Page 1"}, lines={page_1}, style={styles.list_2} />

And to then, in LogComponent do something like: 然后,在LogComponent中执行以下操作:

<List style={style}> ... </List>

I also tried using something like 我也尝试使用类似

<List className={list_1}> ... </List>

But none of the things I've tried works... 但是我尝试过的所有方法都不起作用...

Log.js Log.js

import React from 'react'
import Typography from '@material-ui/core/Typography'
import { withStyles } from '@material-ui/core/styles'
import LogComponent from './LogComponent'

const styles = theme => ({
  title: {
    padding: theme.spacing.unit*1.5,
  },
  list_1: {
  },
  list_2: {
    width: "300px"
  },
  listContainer: {
    flexGrow: 1,
    minHeight: 0,
    overflow: 'auto'
  },
})

const Log = ({classes, log}) => {
  const page_1 = log[0];
  const page_2 = log[1];
  return (
    <div>
      <Typography className={classes.title} color="textSecondary" key={1}>
        Example Log
      </Typography>
      <div className={classes.listContainer} key={2}>
        <LogComponent heading={'Page 1'} lines={page_1} />
        <LogComponent heading={'Page 2'} lines={page_2} />
      </div>
    </div>

export default withStyles(styles)(Log)

LogComponent.js LogComponent.js

import React from 'react'
import Typography from '@material-ui/core/Typography'
import { withStyles } from '@material-ui/core/styles'
import { List, ListItem, ListItemText } from '@material-ui/core';

const styles = theme => ({
  title: {
    padding: theme.spacing.unit*1.5,
  },
}

const LogComponent = ({classes, list_class, heading, lines}) => {

    return (
    <div className={classes.root}>
    <Typography className={classes.title} color="textSecondary" key={1}>
            {heading}
            </Typography>
            <div>
            <List dense>
                {[...lines[0]].map(e => 
                <ListItem><ListItemText primary={e} /></ListItem>
                )}
            </List>
            </div>                    
    </div>
    )
}

export default withStyles(styles)(LogComponent)

Here you are sending the styles as a prop to LogComponent , that's why it will not be applied as styles to that component you have created. 在这里,您将样式作为prop发送给LogComponent ,这就是为什么它不会作为样式应用于您创建的组件的原因。 The style attribute is for HTML tags and in material-ui, you can pass styles to a wrapper component also. style属性用于HTML标记,在material-ui中,您也可以将样式传递给包装器组件。

In your case you can get the styles inside your LogComponent as below: 在您的情况下,您可以在LogComponent获取样式,如下所示:

Send styles as a prop as you mentioned in the question 如您在问题中提到的那样发送样式作为道具

<LogComponent heading={"Page 1"}, lines={page_1}, style={styles.list_1} />

Now, you can access it from props, 现在,您可以通过道具访问它,

                                                   // right below get the style
const LogComponent = ({classes, list_class, heading, lines, style}) => {

return (
<div className={classes.root} style={style}> // Look style attribute added, style(value) is from props
<Typography className={classes.title} color="textSecondary" key={1}>
     {heading}
     </Typography>
     <div>
     <List dense>
          {[...lines[0]].map(e => 
                <ListItem><ListItemText primary={e} /></ListItem>
           )}
      </List>
      </div>                    
    </div>
    )
}

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

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