我是 React 新手,我可以在 const 中设置样式吗
这是我的代码
const initialStudentCounter =
'Students' + ' ' + JSON.stringify(studentCountInitial);
我需要设置“JSON.stringify(studentCountInitial)”的样式
我尝试过这个方法,但没有成功
const initialStudentCounter =
'Students' + ' ' + <span style={color : 'red'}>JSON.stringify(studentCountInitial)</span> ;
有人可以帮我吗
最佳答案
在 JSX 中您不需要 JSON.stringify
。您确实需要将其与周围的元素一起插入 JSX 中。您也不应该再需要 +
来连接字符串。
export default function App() {
const studentCountInitial = 4;
const initialStudentCounter = <div>Students <span style={{color : 'red'}}>{studentCountInitial}</span></div>;
return (
initialStudentCounter
);
}
关于javascript - 我们可以在 React 中设置常量内部的样式吗,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65867512/