You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

React Native WebView返回时表单数据重置问题求助

问题:React Native WebView 返回后表单数据丢失

在React Native应用中使用react-native-webview展示问卷链接,WebView内包含文本框、复选框等表单输入项。用户点击WebView内的"数据隐私"链接跳转至其他页面后返回,所有表单数据(含复选框状态)均被清空重置,需要实现WebView在返回时保留导航历史及已填写的表单数据。

现有代码

// imports

const Questionnaire = ({ currentVisit, email }) => {
  const [loading, setLoading] = React.useState(true)
  const [token, setToken] = React.useState('')
  const { t } = useTranslation()
  const [canGoBack, setCanGoBack] = useState(false)
  const webViewRef = useRef(null)
  const theme = useTheme()
  const styles = makeStyles(theme.colors)

  useEffect(() => {
    if (Platform.OS !== 'web' && !currentVisit?.questionnaireLink.includes('quest.app.com')) { // don not change orientation for quest.app.com
      changeScreenOrientationToLandscape()
    }
  }, [currentVisit?.questionnaireLink])

// some other code

  return (
    !isInternetReachable
      ? <InfoPage text={t('no_connection')} />
      : <WebView
          ref={webViewRef}
          style={{ marginTop: Constants.statusBarHeight }}
          source={{ uri: currentVisit?.questionnaireLink + (token === '' ? '' : `?token=${token}&email=${encodeURIComponent(email)}`) }}
          allowsBackForwardNavigationGestures // ios only
          setBuiltInZoomControls={false}
          onLoadProgress={event => {
            setCanGoBack(event.nativeEvent.canGoBack)
          }}
          startInLoadingState
          renderLoading={() => <ActivityIndicator style={styles.webViewLoadingIndicator} />}
        />
  )
}

/**
 * Locks Screen Orientation to landscape
 */
async function changeScreenOrientationToLandscape () {
  await ScreenOrientation.lockAsync(ScreenOrientation.OrientationLock.LANDSCAPE_LEFT)
}

/**
 * Locks Screen Orientation to portrait
 */
async function changeScreenOrientationToPortrait () {
  await ScreenOrientation.lockAsync(ScreenOrientation.OrientationLock.PORTRAIT_UP)
}

export default Questionnaire

已尝试的无效方案

  • cacheEnabled:开启WebView缓存,无法保留表单数据
  • saveFormDataDisabled={false}:允许表单数据持久化,无效果
  • incognito={false}:禁用隐身模式以保留会话数据和Cookie,问题依旧
  • sharedCookiesEnabled={true}:启用WebView实例间Cookie共享,未解决问题

内容的提问来源于stack exchange,提问作者Shayimerdan

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.14 20:12:34