我发现这样的代码段在我的服务器上运行完美:
const Button = (props) => {
const { className, disabled, onClick, children, ...rest } = props
return (
<button type="button"
className={classNames('btn', className, disabled && 'disabled')}
disabled={disabled}
onClick={(e) => !disabled && onClick(e) }
{...rest}>
{children}
</button>
)
}
这是一个 nodejs 脚本。我认为它应该是这样的:
const Button = (props) => {
const { className, disabled, onClick, children, ...rest } = props
return (
`<button type="button"
className={classNames('btn', className, disabled && 'disabled')}
disabled={disabled}
onClick={(e) => !disabled && onClick(e) }
{...rest}>
{children}
</button>`
)
}
那么在服务器端,我可以省略 `
符号吗?
答案 0 :(得分:1)
不,您不能省略服务器端的 ` 符号。
您显示的第一个片段不是 HTML。它们是 JSX 语法。服务器代码正在由 TypeScript 或 Babel 或类似的转译器转译。
如果它是由 TypeScript 转译的,代码将有一个启用 JSX transpilation 的 tsconfig.json
。
如果它是由 Babel 转译的,那么 babel 配置将在配置插件树的某处包含 this one 或类似的 JSX 插件。
即使您没有使用任何转译器,在代码中添加 ` 符号也不会产生有效的语法,因为那不是有效的 HTML。