【问题标题】:How to set the style a react.js component when creating it?react.js 组件创建时如何设置样式?
【发布时间】:2018-07-25 13:39:20
【问题描述】:

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

下面是我的一些代码(部分继承自更强大的开发人员,然后为简洁起见进行了简化)。

我想重新使用我的 LogComponent 来打印多页日志。但是,在某些情况下,我想在返回的 List 上强制使用特定的宽度,而不是让它按照它认为合适的方式弯曲。

我宁愿不定义单独的 LogComponentFixed 或在我的 LogComponent 中使用 if (...) {return (...)} else {return(...)}

我打算在 Log.js 中做一些事情,例如:

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

然后,在 LogComponent 中执行以下操作:

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

我也尝试过使用类似的东西

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

但是我尝试过的所有方法都不起作用......

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

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)

【问题讨论】:

  • 我可以看到您正在使用来自material-uiwithStyles。如果您的实施是强制性的,您应该将其添加为标签和问题描述中
  • 好的,谢谢:我添加了。不确定我会考虑它的强制性,但由于它在当前代码中,它也可能在标签中。

标签: javascript css reactjs material-ui react-with-styles


【解决方案1】:

在这里,您将样式作为prop 发送到LogComponent,这就是为什么它不会作为样式应用于您创建的组件的原因。 style 属性用于 HTML 标签,在 material-ui 中,您也可以将样式传递给包装器组件。

在您的情况下,您可以在 LogComponent 中获取样式,如下所示:

您在问题中提到的将样式作为道具发送

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

现在,你可以通过 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>
    )
}

【讨论】:

  • 谢谢 我试过了,但还没有完全奏效。在(非简化的)实际代码中,LogComponent 包含 2 个列表,我希望第一个具有固定宽度,因此我需要将样式应用于 &lt;List&gt; 组件,而不是 &lt;div&gt; 组件,如你的例子。我已经尝试过了,我没有收到任何错误,但我可以看到样式没有被应用......
  • 如果你通过 props 传递所需的样式,无论你在哪里应用它都应该可以正常工作。也许在浏览器中检查样式是否显示有错误。
  • 在浏览器中,我单击检查元素,但我在那里看不到“宽度”关键字的概念。那是我应该寻找的吗?
  • @levraininjaneer 转到这个CodeSandbox 链接,我已经创建了一个实现
  • 你就是男人!这样可行。这次不同的是,我在创建组件时明确设置了样式并使用了双花括号,而之前我尝试将其传递给预定义的样式。所以我这样做:&lt;LogComponent heading={"Some Heading"} lines={some_lines} style={{width: "400px"}} /&gt;。我仍然很想知道如何在顶部定义样式,然后传递它,例如 &lt;LogComponent heading={"Some Heading"} lines={some_lines} style={the_style_defined_above} /&gt; ...你能把它放在沙箱中吗?
猜你喜欢
  • 2017-01-27
  • 2023-01-07
  • 2018-03-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-09-09
  • 2021-10-21
  • 1970-01-01
相关资源
最近更新 更多