简体   繁体   English

如何在 React JS 中的 h1 中显示输入值

[英]how can I display input value in h1 in React JS

I have one input which divides 2 number and gives value, so I want to display that value in h1 tag我有一个输入除以 2 个数字并给出值,所以我想在 h1 标记中显示该值

 <input className='input' type="number" id="" placeholder='$' onChange={(evt) => {console.log(evt.target.value / coinId.price)}}/>

coinId.price is todays BTC price from API but it is not important at this point coinId.price 是今天来自 API 的 BTC 价格,但此时并不重要

here is code which gave me result in console log so any suggestions?这是在控制台日志中给我结果的代码,所以有什么建议吗? Thanks.谢谢。

You forgot the value attribute I think, and if you use React Hooks maybe you can do something like this :你忘记了我认为的value属性,如果你使用 React Hooks 也许你可以做这样的事情:

import { useState } from "react";

const [inputValue, setInputValue] = useState("");

...


<input className="input" type="number" placeholder="$" onChange={(evt) => setInputValue(evt.target.value / coinId.price)} value={inputValue} />```

One of the methods that I can think of is that, by using useState, set a state like我能想到的一种方法是,通过使用 useState,设置一个像

const (myAns,setMyAns) = useState(0)

<input className='input' type="number" id="" placeholder='$' onChange={(evt) => {setMyAns(evt.target.value / coinId.price)}}/>

<h1>{myAns}</h1>

You can also use useEffect, so that whenever price of bitcoin is changed, you may get updated value.你也可以使用 useEffect,这样每当比特币的价格发生变化时,你就可以获得更新的价值。

Or或者

<h1><input className='input' type="number" id="" placeholder='$' onChange={(evt) => {setMyAns(evt.target.value / coinId.price)}} value={inputValue} /></h1>

For furthur reference if case, how to make a input field value in h1?如需进一步参考, 如何在 h1 中创建输入字段值?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 ReactJS:我如何对齐表单<Field>的<input>并且<button>和</button><h1><button>在一排里面</button><div> <button>?</button> - ReactJS: How can I align form <Field>'s <input> and <button> and <h1> in a row inside a <div>? 如何更改字体大小<h1>在反应引导上校?</h1> - How can I change the font size for a <h1> in react bootstrap col? 如何创建复制到剪贴板?从 h1 标签复制数据到输入(反应) - How to create Copy to Clipboard?copy data from h1 tag to input (React) React-hooks。 如何使输入显示并在 setstate 值后获得输入的焦点?谢谢 - React-hooks. How can I make the input display and get the focus of the input after the setstate value?Thanks 如何创建可用作 h1、h2、h3、h4..etc 的 Typography 组件 - How do I create a Typography component that can be used as h1, h2, h3, h4..etc 如何将输入值作为标签添加到对象数组并在反应js中显示 - How to add input value as a tag to an object array and display it in react js 我的应用程序没有呈现一个简单的<h1>在反应js中标记 - My application is not rendering a simple <h1> Tag in react js 在 React 中加载页面时未显示 H1 标记值 - H1 tag value not showing when page loads up in React 使用钩子在反应中单击 h1 标签时如何获取另一个组件? - How to get another component when I click h1 tag in react using hooks? 如何在 react js 的表格网格中显示键值对? - How can i display key value pairs in a table grid in react js.?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM