[英]How to refer to another class from a function in React JS?
I am relative new with JS so can someone please help me with the below problem in React JavaScript and explain how to do the followings? 我是JS的新手,所以有人可以帮助我解决React JavaScript中的以下问题并解释如何执行以下操作吗?
I wish to create a program which either list all the available DB records (credit card items) or add new items to DB (newly registered cards). 我希望创建一个程序,该程序列出所有可用的数据库记录(信用卡项目)或将新项目添加到数据库(新注册的卡)。
I have created 3 JS classes, one for listing the already registered items, one for adding new ones, and the third one with 2 buttons where user can choose either to pay with an existing card or with a new one. 我创建了3个JS类,一个用于列出已注册的项目,一个用于添加新项目,第三类带有2个按钮,用户可以选择使用现有卡或新卡付款。
After clicking the button, unfortunately I don't know how to call the given classes (either ExistingCardList.js
or NewCard.js
) to proceed. 单击按钮后,不幸的是,我不知道如何调用给定的类(
ExistingCardList.js
或NewCard.js
)继续进行。
I marked the affected classes with yellow: picture 我用黄色标记了受影响的班级: 图片
I have written something like this: 我写了这样的东西:
export default class CreditCardForm extends React.Component {
constructor (props) {
super(props)
this.handleExistingCard = this.handleExistingCard.bind(this)
this.handleNewCard = this.handleNewCard.bind(this)
}
handleExistingCard() {
return(
<ExistingCardList /> )
}
handleNewCard = () => {
//return(
<NewCard />
}
render() {
return (
<div>
<Form>
<FormGroup>
<Button style={{ margin: 50, marginLeft: 300, paddingLeft: 100, paddingRight: 100 }} onClick={this.handleExistingCard} >Pay with existing card</Button>
<Button style={{ paddingLeft: 100, paddingRight: 100 }} onClick={this.props.handleNewCard}>Pay with a still not registered card</Button>
</FormGroup>
</Form>
</div>
)
}
Thanks a lot! 非常感谢!
You can set one boolean state so within this state you can show your component. 您可以设置一个布尔状态,以便在此状态下可以显示组件。
export default class CreditCardForm extends React.Component {
constructor (props) {
super(props)
this.state = {
isNewCard: false
}
this.handleExistingCard = this.handleExistingCard.bind(this)
this.handleNewCard = this.handleNewCard.bind(this)
}
handleExistingCard() {
this.setState({ isNewCard: false });
}
handleNewCard = () => {
this.setState({ isNewCard: true })
}
render() {
return (
<div>
<Button onClick={this.handleExistingCard} >Pay with existing card</Button>
<Button onClick={this.handleNewCard}>Pay with a still not registered card</Button>
<div>
{this.state.isNewCard ?
<NewCard />
:
<ExistingCardList />
}
</div>
</div>
)
}
}
Hope this will help 希望这会有所帮助
Have you imported the referred classes in the current file? 您是否在当前文件中导入了引用的类?
import ExistingCardList from './ExistingCardList'
import NewCard from './NewCard'
Also do those classes have render()
methods declared in them? 这些类中是否也声明了
render()
方法?
To display/hide a component after the user action you can do something like 要在用户操作后显示/隐藏组件,您可以执行以下操作
import NewCard from './NewCard';
import ExistingCardList from './ExistingCardList';
Class Main extends React.Component{
state = {
option: false,
card: false // this will be changing to existing/new use this value for anything else
}
selectCard(card){
this.setState({card: card});
}
selectOption(option){
this.setState({option: option})
}
render(){
return (
<div className="payment">
<div className="options">
<ul>
<li onClick={this.selectOption.bind(this, 'card')}>Card</li>
<li onClick={this.selectOption.bind(this, 'paypal')}>PayPal</li>
</ul>
</div>
{this.state.option === 'card' && <div className="card-selector">
<div className="buttons">
<button onClick={this.selectCard.bind(this, 'existing')}>Pay with existing card</button>
<button onClick={this.selectCard.bind(this, 'new')}>Pay with a new card</button>
</div>
<div className="card">
{(this.state.card === 'new')? <NewCard /> : <ExistingCardList />;}
</div>
</div>}
</div>
)
}
}
Let me know if you need any further info. 让我知道您是否需要其他信息。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.