简体   繁体   English

如何在 React 中更改 InputBase 的占位符字体/颜色?

[英]How do I change my InputBase's placeholder font/color in React?

I tried to do it with useStyles.我试着用 useStyles 来做。 I have the following code:我有以下代码:

// imports above here

const useStyles = makeStyles((theme) => ({
  placeholder: {
    color: 'black',
    fontSize: 'large',
  },
}));


export default function Chatbox() {
  const classes = useStyles;
  const {currentChannel} = React.useContext(globalContext);

  const [placeholder, setPlaceholder] =
    React.useState('Message ' + Object.values({currentChannel})[0]);

  useEffect(() => {
    setPlaceholder('Message ' + Object.values({currentChannel})[0]);
  }, [currentChannel]);


  return (
    <Paper
      component="form"
      sx={{
        p: '4px 8px', display: 'flex', alignItems: 'center',
        flexShrink: 1, width: 1670}}>
      <InputBase
        sx={{ml: 4, flex: 1}}
        placeholder={placeholder}
        inputProps={{'aria-label': 'chatbox'}}
        className= {classes.placeholder}
      />
      <Divider sx={{height: 48, m: 0.5}} orientation="vertical" />
      <IconButton sx={{p: '25px'}} aria-label="send">
        <SendIcon />
      </IconButton>
    </Paper>
  );
}

However, my placeholder size and color never change even after changing the values in useStyles.. It stays grey and the same size.但是,即使更改了 useStyles 中的值,我的占位符大小和颜色也不会改变。它保持灰色和相同的大小。

You forgot to call the function.你忘了打电话给 function。

// imports above here

const useStyles = makeStyles((theme) => ({
  placeholder: {
    color: 'black',
    fontSize: 'large',
  },
}));


export default function Chatbox() {
  const classes = useStyles();      // <--------- Here 
  const {currentChannel} = React.useContext(globalContext);

  const [placeholder, setPlaceholder] =
    React.useState('Message ' + Object.values({currentChannel})[0]);

  useEffect(() => {
    setPlaceholder('Message ' + Object.values({currentChannel})[0]);
  }, [currentChannel]);

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

相关问题 react.js 中 InputBase#s 占位符组件的颜色更改不起作用 - Color change not working for InputBase#s placeholder component in react.js 如何更改此代码块中的字体大小和字体颜色? - How do I change the font size and font color in this block of code? 如何改变 <input/> 的占位符颜色? - How to change <input/>'s placeholder color? 如何更改Vue中v-text-filed占位符的字体颜色? - How to change the font color of placeholder of v-text-filed in Vue? 如何在 React 中更改字体颜色、字体大小和按钮颜色 - how to change the font color, font size, and button color in React 如何居中,更改此字符串的颜色和字体? - How do I center, change the color and font of this string? 如何更改对象中特定值的字体颜色? - How do I change the font color of a specific value in an object? 如何使用 Element UI 从我的 el-input 更改占位符颜色 - How can I change the placeholder color from my el-input with Element UI 如何使用 useState 钩子将我的反应图标的颜色更改为蓝色? - How do I use the useState hook to change the color of my react icons to blue? 如果提交了某个值,如何更改输入类型 =“文本”中占位符文本的颜色? - How do i change the color of the placeholder text in an input type=“text” if a certain value is submitted?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM