Reactjs 如何使用React+;打字稿?
我有一个锚定组件,我想有条件地设置其样式:Reactjs 如何使用React+;打字稿?,reactjs,typescript,Reactjs,Typescript,我有一个锚定组件,我想有条件地设置其样式: <a className="menu-item" role="button" href="/#" style={renderChangeColor} > {changeStr} </a> const renderChangeColor = () => { let style = {}; if (changeSign === POSITIVE) { style["colo
<a
className="menu-item"
role="button"
href="/#"
style={renderChangeColor}
>
{changeStr}
</a>
const renderChangeColor = () => {
let style = {};
if (changeSign === POSITIVE) {
style["color"] = "green";
} else if (changeSign === NEGATIVE) {
style["color"] = "red";
} else {
style["color"] = "white";
}
return style;
};
您需要向样式中添加一个类型,因为否则,它是
隐式any
,并且由于初始对象没有值,所以颜色是可选的(因此我使用?)
const renderChangeColor=()=>{
让样式:{color?:string}={};//对于类型,样式不是任何。
if(changeSign==正){
样式[“颜色”]=“绿色”;
}else if(changeSign==负){
样式[“颜色”]=“红色”;
}否则{
样式[“颜色”]=“白色”;
}
回归风格;
};代码>您需要将类型添加到样式中,否则,它是隐式的,并且由于初始对象没有值,所以颜色是可选的(因此我使用?)
const renderChangeColor=()=>{
让样式:{color?:string}={};//对于类型,样式不是任何。
if(changeSign==正){
样式[“颜色”]=“绿色”;
}else if(changeSign==负){
样式[“颜色”]=“红色”;
}否则{
样式[“颜色”]=“白色”;
}
回归风格;
};代码>您需要添加类型并调用函数renderChangeColor
(不仅仅是提供参考)
样式修复
const renderChangeColor=()=>{
let style:React.cssprroperties={};//您需要添加类型并调用函数renderChangeColor
(而不仅仅是提供参考)
样式修复
const renderChangeColor=()=>{
让style:React.cssprroperties={};//嘿,我做了那个编辑,现在我得到了这个错误:类型'()=>{color?:string | undefined;}'与类型'CSSProperties'没有共同的属性。您想调用它吗?TS2560
Nevermind,发现了我的问题。我没有调用该函数。谢谢!嘿,我进行了编辑,现在出现了以下错误:类型的值'()=>{color?:string | undefined;}'与类型'CSSProperties'没有共同的属性。您想调用它吗?TS2560
Nevermind,发现了我的问题。我没有调用该函数。谢谢!
TypeScript error in /Users/coinflex/coinflex/markets/src/components/Trade/MarketOverview/AssetSelector/AssetSelectorRow/AssetSelectorRow.tsx(32,7):
Element implicitly has an 'any' type because expression of type '"color"' can't be used to index type '{}'.
Property 'color' does not exist on type '{}'. TS7053
30 | let style = {};
31 | if (changeSign === POSITIVE) {
> 32 | style["color"] = "green";
| ^
33 | } else if (changeSign === NEGATIVE) {
34 | style["color"] = "red";
35 | } else {