如果我有一个需要使用和操作多个状态项的功能组件,并且我有多个状态用于不直接相关的不同事物(例如游戏统计数据和 UI 元素状态),那么性能(或实践)是否会更好?像这样做:
const [state, setState] = React.useState({
username: 'JoeSchmoe200',
points: 200,
isHoveringOverItem: {item1: false, item2: false, item3: false},
selectedItem: {item1: true, item2: false, item3: false}
})
Run Code Online (Sandbox Code Playgroud)
(状态中保存的所有内容都在一个对象中并使用一种方法设置),或者像这样:
const [username, setUsername] = React.useState('JoeSchmoe200')
const [points, setPoints] = React.useState(200)
const [isHoveringOverItem, setIsHoveringOverItem] = React.useState(
{item1: false, item2: false, item3: false}
)
const [selectedItem, setSelectedItem] = React.useState(
{item1: true, item2: false, item3: false}
)
Run Code Online (Sandbox Code Playgroud)
其中每个状态都单独声明并单独设置。我只是想在这里引发一场对话,以了解有关 React 的更多信息。
对于这样的表现,您有何看法?可读性方面?这是偏好问题还是有客观的最佳实践?
我正在尝试使用 Vite 和插件将 SVG 作为 ReactComponent 导入vite-plugin-svgr,但遇到了一个我认为与类型相关的错误。
这是我的导入声明和用法:
import { ReactComponent as ProfileIcon } from '../assets/profile.svg'
export function Example() {
return (
<div>
<p>blahblahblah</p>
<ProfileIcon />
<div>
}
}
Run Code Online (Sandbox Code Playgroud)
该页面根本无法加载,并且我的浏览器控制台显示以下错误:
未捕获的语法错误:不明确的间接导出:ReactComponent
我认为这与我的custom.d.ts文件有关,其中包含以下声明:
declare module '*.svg' {
import * as React from 'react'
export const ReactComponent: React.FunctionComponent<
React.ComponentProps<'svg'> & { title?: string }
>
export default ReactComponent
}
Run Code Online (Sandbox Code Playgroud)
我在解决另一个 TypeScript 错误后添加了此声明,许多 StackOverflow 用户发布了相同的建议,但这里有一个示例。
我似乎找不到适合我的 React + Vite + TypeScript SVG 组件指南。
作为参考,以下是我所做的一些其他更改:
tsconfig.json
{
"compilerOptions": { …Run Code Online (Sandbox Code Playgroud) 好的,所以我正在尝试使用Django/Python创建一个随机数生成器网页.我需要完成的是以某种方式在我的HTML模板文件中使用python代码,除了我无法找到如何做到这一点.
<h1 style="font-size:50px;line-height:20px;color:rgb(145,0,0);font- family: Arial Black, Gadget, sans-serif"></h1>
<h2 style="line-height:10px;color:rgb(140,140,140)"></h2>
<h3 style="font-size:40px;line-height:10px;font-family: Arial Black, Gadget, sans-serif"></h3>
<body style="background-color:rgb(255,239,154)"></body>
<!--Style placeholders-->
<h1 style="text-align:center;position:relative;top:20px">
Test Site
</h1>
<!--Reroll icon-->
<h1 style="text-align:center;position:relative;top:20px">
<input type='image' style='width:60px;height:56px;' src='../../static/polls/dice.png' alt='Re-roll'
onclick='location.reload();' value='Roll' /></h1>
Run Code Online (Sandbox Code Playgroud) 有没有一种方法可以计算某个范围内的项目数(即公式)?
我只期待=TEXT公式,所以我尝试了=COUNTIF(1:1, "=TEXT"),但这没有用。似乎 CountIf 仅对单元格的显示值进行操作。
reactjs ×2
django ×1
excel ×1
html ×1
javascript ×1
python ×1
react-hooks ×1
typescript ×1
vite ×1