I'm using React and Material UI for a project, and I'm implementing payment methods. I will basically support different type of payment methods, and depending which one user chooses I want to display a form, and once the user fills the form and submits it, I want to return to my previous view. I have a component that looks like this:
import React, { Component } from 'react';
import { List, ListItem } from 'material-ui';
import { Button } from '../../components';
import StripeForm from './stripe/form';
import PropTypes from 'prop-types';
class MyComp extends Component {
constructor(props) {
super(props);
this.state = {
paymentProviders: [],
indexOfClickedItem: -1,
};
this.onListItemClicked = this.onListItemClicked.bind(this);
this.onPayment = this.onPayment.bind(this);
}
onListItemClicked(paymentProvider, index) {
let paymentProviders = {
providerName: paymentProvider.ProviderName,
publicKey: paymentProvider.PublicKey,
};
this.setState({
paymentProviders: paymentProviders,
indexOfClickedItem: index,
});
}
onPayment() {
switch (this.state.paymentProviders.providerName) {
case "stripe":
// render the form for stripe payment
return ( <StripeForm /> );
case "paypal":
// render the form for paypal payment
default:
return null;
}
}
render() {
return (
<div>
<div>
<List>
{this.props.paymentProviders.map((pp, index) => {
return (
<ListItem key={pp.ProviderName}
primaryText={pp.ProviderName}
onClick={() => this.onListItemClicked(pp, index)}
/>
)
})}
</List>
</div>
<div>
<Button onClick={this.onPayment}
label="Pay" />
</div>
</div>
);
}
}
MyComp.propTypes = {
paymentProviders: PropTypes.array.isRequired,
};
export default MyComp;
So, basically, depending on which ListItem is clicked I update my indexOfClickedItem in the state, and then also update some details about the payment provider. But, what I mainly want to achieve is that, when my button is clicked, depending on which payment provider is chosen (meaning which list is previously clicked/selected), I want to render another component. Any ideas how to achieve it?
Switchrender() => { var show = this.state.modalIsOpen; return ( {show === true && <Modal /> } {show === false && <Modal2 /> } ) }See here for reference: reactjs.org/docs/conditional-rendering.html