From b40962486f419e1dc6b75dec26194c6f9d1c6c7d Mon Sep 17 00:00:00 2001 From: lareinayanyu Date: Tue, 5 Nov 2024 20:20:51 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20form=20=E8=B0=83=E7=94=A8=20switch=20get?= =?UTF-8?q?Value=E5=8F=96=E5=80=BC=E4=B8=8D=E6=AD=A3=E7=A1=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../runtime/components/react/mpx-switch.tsx | 20 ++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) 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,