onChange 在通过状态更新更改值时在反应功能组件中不起作用

发布于 2025-01-19 06:01:54 字数 1392 浏览 2 评论 0原文

我正在一个项目中使用MAPBOX,在这里,我正在尝试从输入框中获得纬度和经度。我已经在输入字段中设置了一个值,我想在编写内容时更改它,但是在尝试编写某些内容时,输入框中没有任何更改。

请检查代码

 const [marker, setMarker] = useState({
    latitude: 20.2961,
    longitude: 85.8245
  });

这是持续的输入。

           <div className='flex gap-6'>
                <div className='relative w-1/2'>
                  <input onChange={(e) => setMarker({latitude:e.target.value, ...marker})}  value={marker.latitude} type="number" placeholder='Latitude' className='py-4 w-full pl-12 text-xs rounded-md focus:outline-none border border-bottom-border' name="" id="" />
                  <MapPin className='absolute top-1/2 -translate-y-1/2 left-2 bg-[#0000000d] p-2 rounded text-gray-500' size={30} weight="bold" />
                </div>
                <div className='relative w-1/2'>
                  <input onChange={(e) => setMarker({longitude:e.target.value, ...marker})} value={marker.longitude} type="number" placeholder='Longitude' className='py-4 w-full pl-12 text-xs rounded-md focus:outline-none border border-bottom-border' name="" id="" />
                  <MapPin className='absolute top-1/2 -translate-y-1/2 left-2 bg-[#0000000d] p-2 rounded text-gray-500' size={30} weight="bold" />
                </div>
              </div>

如果您知道解决方案,请提供帮助。

I am using mapbox in a project and here I am trying to get the latitude and longitude from the input box. I have set a value in input field and I want to change it while writing the but nothing is changing in the input box while trying to write something.

Please check the code

 const [marker, setMarker] = useState({
    latitude: 20.2961,
    longitude: 85.8245
  });

This is the rended input.

           <div className='flex gap-6'>
                <div className='relative w-1/2'>
                  <input onChange={(e) => setMarker({latitude:e.target.value, ...marker})}  value={marker.latitude} type="number" placeholder='Latitude' className='py-4 w-full pl-12 text-xs rounded-md focus:outline-none border border-bottom-border' name="" id="" />
                  <MapPin className='absolute top-1/2 -translate-y-1/2 left-2 bg-[#0000000d] p-2 rounded text-gray-500' size={30} weight="bold" />
                </div>
                <div className='relative w-1/2'>
                  <input onChange={(e) => setMarker({longitude:e.target.value, ...marker})} value={marker.longitude} type="number" placeholder='Longitude' className='py-4 w-full pl-12 text-xs rounded-md focus:outline-none border border-bottom-border' name="" id="" />
                  <MapPin className='absolute top-1/2 -translate-y-1/2 left-2 bg-[#0000000d] p-2 rounded text-gray-500' size={30} weight="bold" />
                </div>
              </div>

Please help if you know the solution.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

南薇 2025-01-26 06:01:54

这里应该首先使用扩展运算符。使用这样的东西

onChange={(e) => setMarker({...marker, latitude:e.target.value })}

The spread operator should be used first here. Use something like this

onChange={(e) => setMarker({...marker, latitude:e.target.value })}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文