javascript - 如何在另一个组件上使用来自组件的信息
问题描述
我正在尝试在我的应用程序中创建一个支付系统。
我有一个付款表格(paymentForm.js),它包含付款信息(卡号、cvv、到期...)。有什么方法可以让我在另一个组件(checkoutPage.js)上获取这些信息?你有什么建议吗?
下面是我的 paymentForm.js:
export default class PaymentForm extends React.Component {
state = {
cvv: '',
expiry: '',
focus: '',
name: '',
number: '',
};
handleInputFocus = (e) => {
this.setState({ focus: e.target.name });
}
handleInputChange = (e) => {
const { name, value } = e.target;
this.setState({ [name]: value });
}
render() {
return (
<View>
<Cards id="PaymentForm"
cvc={this.state.cvc}
expiry={this.state.expiry}
focused={this.state.focus}
name={this.state.name}
number={this.state.number}
/>
<form style={{}}>
<input
type="tel"
name="number"
placeholder="Card Details"
maxLength="16"
preview='true'
onChange={this.handleInputChange}
onFocus={this.handleInputFocus}
/>
<br/>
<input
type="text"
name="name"
placeholder="Holder Name"
onChange={this.handleInputChange}
onFocus={this.handleInputFocus}
/>
<br/>
<input
type="tel"
name="expiry"
placeholder="Expiration"
maxLength="4"
onChange={this.handleInputChange}
onFocus={this.handleInputFocus}
/>
<br/>
<input
type="tel"
name="cvc"
placeholder="CVV"
maxLength="5"
onChange={this.handleInputChange}
onFocus={this.handleInputFocus}
/>
<br/>
<input
type="tel"
name="zipcode"
placeholder="ZIP Code"
maxLength="5"
onChange={this.handleInputChange}
onFocus={this.handleInputFocus}
/>
</form>
</View>
);
}
}
解决方案
您应该创建一个文件 CheckOut.js 并通过道具提供卡片信息。还有另一种方法是创建一个名为“Checkout”的类并在其中创建静态方法。
推荐阅读
- mysql - 我如何从 codeigniter 上的表中获取 id?
- asp.net-mvc - EF Core 代码首先插入空的父实体
- html - 使用角度和引导程序 3.3.7无法在第一次点击时点击标签
- laravel - 在 php.ini 中启用 dom 文件
- javascript - JavaScript — 数组中充满了一堆“未定义”
- reactjs - 反应:将令牌传递给本地存储后,导航栏没有改变
- r - 如何转换中间有多余字符的日期
- go - MergeSort algorithm with goroutines questions
- c++ - Failed to connect to a SMTP server
- python - python在lua中有类似于_ENV的东西吗?