[英]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.