ds2321 2019-02-01 19:10
浏览 66
已采纳

将表单数据另存为用户类型(React / Postgres)

Given a simple textbox to answer a question, how would you go about saving answers as the user types? Assuming that the answer is upserted.

Using a somewhat naive example of an onChange handler and saving answers using an API within that onChange, I ran into two problems:

  1. Sends what seems to be too many patch requests from client to API since it's every time the input textbox answer changes.
  2. If using postgres upsert or creating your own, then the second request made by the onChange handler may not see the first if the user is typing quickly, creating duplicate records for that textbox (two answers for one question)
  3. I need to execute queries beforehand to get necessary relationship values before updating, so the HTTP wait time is fairly long. Fixing this now, but seems to not address the above two issues.

I thought of only saving once the user presses spacebar, but that seems hacky. Normally of course I'd save after clicking a submit button, but in this case it must be as the user types.

Any suggestions / comments? Cheers.

EDIT:

Also tried saving within onBlur.. But this doesn't always get called depending on where the user clicks.

  • 写回答

1条回答 默认 最新

  • duankuiyuant3940 2019-02-01 19:22
    关注

    Does it have to patch the data for every keypress or can you use a delay?

    For instance...

    User starts typing: Register the event, set a timeout for say 3 seconds

    If the user keeps typing: reset that timeout back to the full 3s

    If the user pauses typing + 3s passes: patch the data with new value

    When the user resumes typing: set another timeout

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥15 stable diffusion
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件