gpt4 book ai didi

javascript - 如何在无状态功能组件上正确使用 React.StatelessFunctionalComponent

转载 作者:行者123 更新时间:2023-11-28 04:10:10 25 4
gpt4 key购买 nike

我需要为react无状态功能组件添加流注释。根据文档,我应该使用 React.StatelessFunctionalComponent<Props>

其签名如下 Ref. :

type StatelessFunctionalComponent = (props: Props) => React.Node

但是我收到了几个errors .

我在这里做错了什么以及为什么?

// @flow
import * as React from 'react'
import moment from 'moment'
import IconWeather from '../../shared/icon/IconWeather'

/* eslint-disable no-undef */
type PropsType = {
+date: number,
+tempMin: number,
+tempMax: number,
+iconCode:number,
+weatherDescription:string
}
/* eslint-enable no-undef */

const ForecastDay = ({ date, tempMin, tempMax, iconCode, weatherDescription }:PropsType):React.StatelessFunctionalComponent<PropsType> => {
const dateFormat = moment.unix(date).format('ddd, MMM D')
const tempMinRounded = Math.round(tempMin)
const tempMaxRounded = Math.round(tempMax)
return (
<div>
<div>{dateFormat}</div>
<div>
<IconWeather code={iconCode} />
</div>
<div>
<div>
{tempMinRounded}&#176;
</div>
<div>
{tempMaxRounded}&#176;
</div>
</div>
<div>
{weatherDescription}
</div>
</div>
)
}
export default ForecastDay

最佳答案

我通过添加找到了解决问题的方法

 const ForecastDay:React.StatelessComponent<PropsType>

并用作返回类型 ReactElement<any>React.Element<*> .

// @flow
import * as React from 'react'
import moment from 'moment'
import IconWeather from '../../shared/icon/IconWeather'

/* eslint-disable no-undef */
type PropsType = {
+date: number,
+tempMin: number,
+tempMax: number,
+iconCode:number,
+weatherDescription:string
}
/* eslint-enable no-undef */

const ForecastDay:React.StatelessComponent<PropsType> = ({ date, tempMin, tempMax, iconCode, weatherDescription }:PropsType):ReactElement<any> => {
const dateFormat = moment.unix(date).format('ddd, MMM D')
const tempMinRounded = Math.round(tempMin)
const tempMaxRounded = Math.round(tempMax)
return (
<div>
<div>{dateFormat}</div>
<div>
<IconWeather code={iconCode} />
</div>
<div>
<div>
{tempMinRounded}&#176;
</div>
<div>
{tempMaxRounded}&#176;
</div>
</div>
<div>
{weatherDescription}
</div>
</div>
)
}
export default ForecastDay

关于javascript - 如何在无状态功能组件上正确使用 React.StatelessFunctionalComponent<Props> ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46365316/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com