属性'joiner'在类型 '{}' 上不存在



尝试设置状态是tsx,但当我尝试从json打开数据时,我得到这个错误返回。

Property 'joiner' does not exist on type '{}'. TS2339

这是组件(删除无用代码)


import Player from '../components/Player'
import listingData from '../libs/listingData'
const Battle = () => {
const [loading, setLoading] = useState(false);
const [creatorState, setCreator] = useState({ health: 100, animate: false, recieve: false });
const [opponentState, setOpponent] = useState({ health: 100, animate: false, recieve: false });
const [data, setData] = useState({});
const loadBattle = () => {
setLoading(true);
const id = parseInt((window.location.pathname).replace("/battle/", ""));
setData(listingData[id])
setLoading(false);
setTimeout(() => triggerAttack("creator"), 5000);
}
useEffect(() => {
loadBattle();
}, [])
return (
<Root>
<Player 
weakness={loading ? '' : data.joiner.weakness} //Error triggered here
image=""
status="filled"
price={2352.54}
health={`${creatorState.health}%`}
creator={false}
recieve={creatorState.recieve}
animate={creatorState.animate}
loading={loading}
/>
</Root>
)
}
export default Battle

Player组件的道具都设置在any

json中对象的示例

price: 5251.52,
status: 'filled',
creator: {
weakness: 'wind',
address: '',
image: '',
},
joiner: {
weakness: 'water',
address: '',
image: '',
},
},

我对打字很陌生,所以我不熟悉类型,它说我没有足够的细节,所以这就是我写的。

在这种情况下,您必须使用包含预期数据结构的接口键入您的状态。如:

的例子:

interface IUser {
name: string;
}
const [user, setUser] = useState<IUser>({name: 'Jon'});

interface Provider {
connected: boolean;
type: string;
}
const [wearablesList, setWearablesList] = useState<Provider[]>([]);

相关内容

  • 没有找到相关文章

最新更新