首页 > 解决方案 > 样式化组件属性分配中的以下代码有什么区别?

问题描述

在下面的1和2之间,将属性分配给HTML元素有什么区别?我用了2,它突然停止工作。1 似乎工作正常。我不知道为什么 2 不再工作了?

使用 1 时出现的错误是: Invalid value for prop size on the tag <input>. Either remove them from the element or pass a string or number value to them in the DOM

//1
const Input = styled.input.attrs(props => ({
    size: props.small ? 5 : undefined,
 }))`

//2
const Input = styled.input.attrs({
    size: (props) => props.small ? 5 : undefined,
 }))`

标签: reactjsstyled-components

解决方案


推荐阅读