英文:
Change css-module dynamically on React
问题
我有一个在组件中的 canvas,如下所示:
当鼠标指针进入 Canvas 时,它可以将指针更改为 crosshair
。
import styles from '../css/basic-styles.module.css';
const ImagePreview = () => {
const [mode, setMode] = useState(0);
function changeMode(mode) {
setMode(mode);
}
return (
<canvas className={styles.canvas} width={200} height={200}></canvas>
);
}
在 CSS 中:
canvas:hover {
/*cursor:pointer;*/
cursor: crosshair;
}
现在我想根据 mode
的值动态更改 CSS。
当 mode
为 1 时,我想使用 pointer
。
我应该使用 css
吗?还是有方法可以使它工作?
英文:
I have a canvas like this in Component
It can change the pointer to crosshair
when cursor is entered on Canvas.
import styles from '../css/basic-styles.module.css';
const ImagePreview = () =>{
[mode,setMode] = useState(0);
changeMode(mode){
setMode(mode);
}
return (){
<canvas className={styles.canvas} width=200 height=200></canvas>
}
}
in css
canvas:hover{
/*cursor:pointer;*/
cursor:crosshair;
}
Now I want to change the css dynamically depending on the mode
value.
I want to use pointer
when mode is 1
I should quite use css
? or is there any method to make it work?
答案1
得分: 1
请查看以下的解决方案。它也可以在沙盒中找到。请忽略为代码片段运行器包含的基本React解决方案。点击“运行代码片段”以在这里预览。
/* 解决方案 */
const ImagePreview = () => {
const [mode, setMode] = useState(0);
return (
<canvas
onMouseEnter={() => setMode(1)}
onMouseLeave={() => setMode(0)}
style={{
backgroundColor: "teal",
cursor: mode ? "crosshair" : "pointer"
}}
width={200}
height={200}
/>
);
};
// 这样可以在Stackoverflow代码片段预览中工作。//
const ImagePreview = () => {
const [mode, setMode] = React.useState(0);
const canvasCfg = {
onMouseEnter: () => setMode(1),
onMouseLeave: () => setMode(0),
style: { backgroundColor: "teal", cursor: mode ? "crosshair" : "pointer" },
width: 200,
height: 200
}
return React.createElement('canvas', canvasCfg)
}
const domContainer = document.querySelector('#app');
const root = ReactDOM.createRoot(domContainer);
root.render(React.createElement(ImagePreview));
<script crossorigin src="https://unpkg.com/react@18/umd/react.production.min.js"></script>
<script crossorigin src="https://unpkg.com/react-dom@18/umd/react-dom.production.min.js"></script>
<div id="app"></div>
<details>
<summary>英文:</summary>
Please see the solution below. It is also available in the [sandbox.][1]. Ignore the vanilla react solution it included for the snippet runner. Click `Run Code Snippet` to see preview here.
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
/* Solution
const ImagePreview = () => {
const [mode, setMode] = useState(0);
return (
<canvas
onMouseEnter={() => setMode(1)}
onMouseLeave={() => setMode(0)}
style={{
backgroundColor: "teal",
cursor: mode ? "crosshair" : "pointer"
}}
width={200}
height={200}
/>
);
};
*/
// This is so it can work in Stackoverflow snippet preview. //
const ImagePreview = () => {
const [mode, setMode] = React.useState(0);
const canvasCfg = {
onMouseEnter: () => setMode(1),
onMouseLeave: () => setMode(0),
style: { backgroundColor: "teal", cursor: mode ? "crosshair" : "pointer"},
width: 200,
height: 200
}
return React.createElement('canvas', canvasCfg)
}
const domContainer = document.querySelector('#app');
const root = ReactDOM.createRoot(domContainer);
root.render(React.createElement(ImagePreview));
<!-- language: lang-html -->
<script crossorigin src="https://unpkg.com/react@18/umd/react.production.min.js"></script>
<script crossorigin src="https://unpkg.com/react-dom@18/umd/react-dom.production.min.js"></script>
<div id="app"></div>
<!-- end snippet -->
[1]: https://codesandbox.io/s/sweet-tdd-4qblsp?file=/src/App.js:58-380
</details>
# 答案2
**得分**: 0
创建两个类似如下的类:
```css
canvas1:hover {
cursor: pointer;
}
canvas2:hover {
cursor: crosshair;
}
并根据模式条件性地使用它们,如下所示:
import styles from '../css/basic-styles.module.css';
const ImagePreview = () => {
const [mode, setMode] = useState(0);
const changeMode = (mode) => {
setMode(mode);
}
return (
<canvas className={mode === 1 ? styles.canvas1 : styles.canvas2} width={200} height={200}></canvas>
);
}
你也可以在没有 :hover
的情况下使用这些类,这也会得到期望的结果,因为光标属性如下:
.canvas1 {
cursor: pointer;
}
.canvas2 {
cursor: crosshair;
}
英文:
create two classes like this
canvas1:hover{
cursor:pointer;
}
canvas2:hover{
cursor:crosshair;
}
and use it conditionally based on mode like
import styles from '../css/basic-styles.module.css';
const ImagePreview = () =>{
[mode,setMode] = useState(0);
changeMode(mode){
setMode(mode);
}
return (){
<canvas className={mode == 1 ? styles.canvas1 : styles.canvas2} width=200 height=200></canvas>
}
}
you can also use classes without :hover
, this will also give the desired result because of cursor property
.canvas1{
cursor: pointer;
}
.canvas2{
cursor: crosshair;
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论