diff --git a/packages/webpack-plugin/lib/runtime/components/react/mpx-switch.tsx b/packages/webpack-plugin/lib/runtime/components/react/mpx-switch.tsx index 29e4fff18..9ae85792d 100644 --- a/packages/webpack-plugin/lib/runtime/components/react/mpx-switch.tsx +++ b/packages/webpack-plugin/lib/runtime/components/react/mpx-switch.tsx @@ -5,7 +5,7 @@ * ✔ color */ import { Switch, SwitchProps, ViewStyle, NativeSyntheticEvent } from 'react-native' -import { useRef, useEffect, forwardRef, JSX, useState, useContext } from 'react' +import { useRef, useEffect, forwardRef, JSX, useState, useContext, useCallback } from 'react' import { warn } from '@mpxjs/utils' import useNodesRef, { HandlerRef } from './useNodesRef' // 引入辅助函数 import useInnerProps, { getCustomEvent } from './getInnerListeners' @@ -51,6 +51,8 @@ const _Switch = forwardRef, _SwitchProps>((prop const changeHandler = bindchange || catchchange + const checkedValueRef = useRef(checked) + let formValuesMap: Map | undefined const formContext = useContext(FormContext) @@ -76,6 +78,10 @@ const _Switch = forwardRef, _SwitchProps>((prop setIsChecked(checked) }, [checked]) + useEffect(() => { + checkedValueRef.current = isChecked + }, [isChecked]) + const nodeRef = useRef(null) useNodesRef(props, ref, nodeRef) @@ -95,13 +101,13 @@ const _Switch = forwardRef, _SwitchProps>((prop } } - const resetValue = () => { + const resetValue = useCallback(() => { setIsChecked(false) - } + }, []) - const getValue = () => { - return isChecked - } + const getValue = useCallback(() => { + return checkedValueRef.current + }, []) useEffect(() => { if (formValuesMap) { @@ -116,7 +122,7 @@ const _Switch = forwardRef, _SwitchProps>((prop formValuesMap.delete(props.name) } } - }, []) + }, []) const innerProps = useInnerProps(props, { ref: nodeRef,