- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
为了练习,我需要构建一个非常简单的货币转换器,仅以设定汇率 (0.91/1.09) 将货币从美元转换为欧元,反之亦然。我希望我的 App.js 文件尽可能模块化。另外,我想使用钩子(Hook)(不是类组件)
我的文件结构:
src
├── components (Holds all components)
│ ├── InputDollar.js (gets Dollar Input)
│ ├── InputEuro.js (gets Euro Input)
│ ├── Button.js (triggers conversion)
│ └── Converter.js (Converts Dollar into Euro or vice versa)
├── App.js (renders all components)
└── Index.js (eventually exports to html "root" id)
我想在这里发布到目前为止我拥有的所有代码会太长,但我共享一个沙箱:https://codesandbox.io/s/misty-morning-l3y1e?fontsize=14
我认为输入和按钮都很好。最有可能有缺陷的是转换器组件 - 我在下面分享 - (因为我很困惑如何传递输入以及如何在 if 语句中编写正确的语法)和应用程序组件,因为我无法在其上显示结果按钮点击。
Converter.js
import React from "react";
import InputDollar from "./InputDollar"
import InputEuro from "./InputEuro"
function ConvertedValue() {
let converted = function() {
if(<InputDollar>!="") {
ConvertedValue = (<InputDollar />* 0.9)
} else if (<InputEuro>!="") {
ConvertedValue = (<InputEuro />* 1.1)
}
}
return (
<div>
{converted}
</div>
);
}
export default ConvertedValue;
您能否帮助我完成此练习,并在可能的情况下注释掉主要功能?
最佳答案
以下是我将如何调整您现有的代码:
1) 一个 <Input>
组件服务于这两个原因。它接受类型和标签以及您的 handleChange
方法。
function Input(props) {
const { label, type, handleChange } = props;
function onChange(event) {
// Pass in the type and value
handleChange(type, event.target.value);
}
return (
<div>
<label>{label}</label>
<input onChange={onChange} type="number" />
</div>
);
}
2) 在您的 <App>
中组件:
function App() {
// Have one state for the converted value
const [ convertedValue, setConvertedValue ] = useState(0);
// Then, when `handleChange` is called, just choose between the
// input type, and set the state
function handleChange(type, value) {
if (type === 'euro') setConvertedValue(value * 1.1);
if (type === 'dollar') setConvertedValue(value * 0.9);
}
return (
<div>
// Using the Input component pass in the type, label, and handleChange
// for the dollar and euro
<Input type="dollar" label="Dollar" handleChange={handleChange} />
<Input type="euro" label="Euro" handleChange={handleChange} />
// Then you can keep the updated converted value here
<div>{convertedValue}</div>
</div>
);
}
希望这有用。
关于javascript - 如何构建一个简单的具有固定转换比率的美元/欧元 react 货币转换器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58115991/
我正在使用 python 正则表达式查找字符串中的所有价格。到目前为止,我只是无法正确管理符号。此代码,输入:'happy$37.54000happy$34$3454$3333€27.80€3.00.
如何使用 imagettftext() 创建 € 符号?我使用的字体“Lucida Grande”包含欧元符号。€ 也不起作用。 最佳答案 参见the documentation (强调我的): Th
我正在使用 AngularJS 货币过滤器,但在正确显示欧元符号时遇到了问题。 HTML: {{ price.priceTotal | currency: myController.getPriceC
为了练习,我需要构建一个非常简单的货币转换器,仅以设定汇率 (0.91/1.09) 将货币从美元转换为欧元,反之亦然。我希望我的 App.js 文件尽可能模块化。另外,我想使用钩子(Hook)(不是类
我有一个 XML 文档 file.xml它以 Iso-latin-15(又名 Iso-Latin-9)编码 €.txt 从我最喜欢的文本编辑器中,我可以看出这个文件以 Iso-Latin-1
我可以将 Alt 键与数字键盘结合使用来键入符号,但如何以编程方式将符号(英镑、欧元、版权)插入文本框? 我有一个配置屏幕,所以我需要动态创建\uXXXX。 最佳答案 在 C# 中,Unicode 字
我使用此 URL 在 CS:GO 容器中一次获取 100 个结果。我用 {currency} 代替 3,用 {start} 代替 100 的倍数,我的问题是 currency=3 code> 似乎不是
我的 Angular 应用程序需要以不同格式显示货币,例如:$、£、2.105,10€ - + 之后的欧元符号不同的分隔符 - 这可以通过包含 i18n angular-locale_de-de.js
我想使用欧元货币构建一个 PayPal 支付 URL。 我在以下位置找到了欧元 (EUR) 的国家/地区代码: https://developer.paypal.com/docs/classic/ap
我是一名优秀的程序员,十分优秀!