1
let [mapPredictionData, setMapPredictionData] = useState({
        prediction_index    : 0,
        prediction_point    : null,
        prediction_point_distance : null
    })

.....some other code

setMapPredictionData({
        prediction_index: prev_index,
        prediction_point: props.missionLocationArray[prev_index],
        prediction_point_distance: get_distance_between_points(props.missionLocationArray[prev_index], currentPosition)
    })

以上是我如何使用 React 钩子 useState 声明我的状态,并遵循我如何设置相同的状态。但是每次我设置状态时它通常都不会改变并且我得到错误 -

“参数类型 {prediction_index: number, prediction_point_distance: number, prediction_point: *} 不可分配给参数类型 ((prevState: {prediction_index: number, prediction_point_distance: null, prediction_point: null}) => {prediction_index: number, prediction_point_distance: null , prediction_point: null}) | {prediction_index: number, prediction_point_distance: null, prediction_point: null} "

我尝试研究解决方案但失败了。有谁知道为什么会这样?

4

0 回答 0