I have an array postArray which is defined in Main.js.
constructor() {
super();
this.state ={
step: 1,
// welcome
qNumber:1,
accountNumber:'',
amount:'',
txNumber:0,
postArray : []
}
}
Also in Main.js, the array is being populated by insertTx arrow function. Need to check if this array is being populated or not...currently i dont know how to use the console.log. This function is being called by CashDeposit.js.
insertTx =() => {
// save transaction to array state
// create copy of the array
const copyPostArray = Object.assign([],this.state.postArray)
// insert one element into the array
copyPostArray.push({
txNumber: this.txNumber,
qNumber : this.qNumber,
accountNumber : this.accountNumber,
amount : this.amount
})
// save the values back to array state
this.setState({
postArray:copyPostArray
})
}
Lastly on Summary.js, which is being called by Main.js, this will display the postArray on the render(). I am confused as to how can I use the map function to populate the array before i can display the contents using .
return(
<>
<h2>Summary of Transactions</h2>
Display content of postArray here
<ul>
{/* {
{this.postArray}
} */}
</ul>
<button className="Back" onClick={this.back}>
« Cancel
</button>
<button className="Next" onClick={this.continue}>
Confirm »
</button>
</>
)
==========
MAIN.JS
import React, {Component} from 'react';
import Welcome from './Welcome';
import Transaction from './Transaction';
import CashDeposit from './CashDeposit';
import AnotherTx from './AnotherTx';
import Summary from './Summary';
import QueNumber from './QueNumber';
export class StepForm extends Component {
constructor() {
super();
this.state ={
step: 1,
// welcome
qNumber:1,
accountNumber:'',
amount:'',
txNumber:0,
postArray : []
}
}
nextStep=()=> {
const {step}=this.state;
this.setState({
step: step + 1
});
}
prevStep=() => {
const {step}= this.state;
this.setState({
step: step -1
});
}
newClient =() => {
const {qNumber}=this.state;
// q number should only increment upon exit
// save the q number on localstorage
this.setState({
qNumber:qNumber+1
});
// force the step as first step
this.setState({
step: 1
});
}
incTxNumber =() => {
const {txNumber}=this.state;
this.setState({
txNumber:txNumber+1
});
}
resetInfo =() => {
// const {accountNumber,amount}=this.state;
this.setState({
accountNumber:'',amount:''
});
}
insertTx =() => {
// save transaction to array state
// create copy of the array
const copyPostArray = Object.assign([],this.state.postArray)
// insert one element into the array
copyPostArray.push({
txNumber: this.txNumber,
qNumber : this.qNumber,
accountNumber : this.accountNumber,
amount : this.amount
})
// save the values back to array state
this.setState({
postArray:copyPostArray
})
}
handleChange=input=> e => {
this.setState({[input]:e.target.value});
}
showStep = () => {
const {step, accountNumber, amount}=this.state;
// txNumber
if (step===1)
return (<Welcome
nextStep={this.nextStep}
handleChange={this.handleChange}
/>);
if(step===2)
return (<Transaction
nextStep={this.nextStep}
prevStep={this.prevStep}
handleChange={this.handleChange}
/>);
if(step===3)
return (<CashDeposit
nextStep={this.nextStep}
prevStep={this.prevStep}
handleChange={this.handleChange}
accountNumber={accountNumber}
amount={amount}
incTxNumber={this.incTxNumber}
insertTx={this.insertTx}
/>);
if(step===4)
return (<AnotherTx
nextStep={this.nextStep}
prevStep={this.prevStep}
handleChange={this.handleChange}
resetInfo={this.resetInfo}
/>);
if(step===5)
return (<Summary
nextStep={this.nextStep}
prevStep={this.prevStep}
handleChange={this.handleChange}
postArray={this.state.postArray}
/>);
if(step===6)
return (<QueNumber
prevStep={this.prevStep}
newClient={this.newClient}
qNumber={this.state.qNumber}
/>);
}
render(){
// const {step}=this.state;
// const {qNumber}=this.state;
// const {txNumber}=this.state;
return(
<>
{/* <h2>Step {step} of 6.</h2>
<h2>THIS IS Q NUMBER {qNumber}</h2>
<h2>Transaction Number {txNumber}</h2> */}
{
this.showStep()}
</>
);
}
}
export default StepForm;
====
SUMMARY.JS
import React, {Component} from 'react';
class Summary extends Component {
continue = e => {
e.preventDefault();
this.props.nextStep();
}
back = e => {
e.preventDefault();
this.props.prevStep();
}
render(){
// const {qNumber, accountNumber, amount}=this.props;
// console.log('our data is',this.props.data)
// var arrayx = [1,4,9,16];
// var array1 =
// const map1 = array1.map(x=>x*2);
// console.log(map1);
// loop thru the data array and do something with each data
// const namesList =data.map(this.txNumber=>{console.log(this.txNumber)}
// txNumber: this.txNumber,
// qNumber : this.qNumber,
// accountNumber : this.accountNumber,
// amount : this.amount
const { postArray } = this.state;
return(
<>
<h2>Summary of Transactions</h2>
Display content of postArray here
<ul>
{postArray.map(({ accountNumber, amount, qNumber, txNumber }) => (
<li key={`${accountNumber}-${txNumber}`}>
<div>
AccountNumber: {accountNumber}
</div>
<div>
Amount: {amount}
</div>
<div>
qNumber: {qNumber}
</div>
<div>
txNumber: {txNumber}
</div>
</li>)
)}
</ul>
<button className="Back" onClick={this.back}>
« Cancel
</button>
<button className="Next" onClick={this.continue}>
Confirm »
</button>
</>
)
}
}
export default Summary;
CashDeposit.js
import React, {Component} from 'react';
class CashDeposit extends Component {
// state={qNumber:234};
continue = e => {
e.preventDefault();
this.props.nextStep();
//increment the txNumber
this.props.incTxNumber();
this.props.insertTx();
}
back = e => {
e.preventDefault();
this.props.prevStep();
}
render(){
const {accountNumber, amount,handleChange}=this.props;
return(
<>
<h2>Cash Deposit</h2>
{/* Queue Number: <b>{qNumber}</b> */}
Please enter account number and amount.
<p>
<label>
Account Number
<input
type="text"
name="accountNumber"
value={accountNumber}
placeholder="Account Number"
onChange={handleChange('accountNumber')}
/>
</label>
</p>
<p>
<label>
Amount
<input
type="text"
name="amount"
value={amount}
placeholder="Amount"
onChange={handleChange('amount')}
/>
</label>
</p>
<button className="Back" onClick={this.back}>
« Back
</button>
<button className="Next" onClick={this.continue}>
Next »
</button>
</>
)
}
}
export default CashDeposit;
Hi @Drew no errors already but when running the code, when Summary of Transactions is displayed, it displays the following values:
Summary of Transactions
Display content of postArray here
ID: NaN AccountNumber: Amount: qNumber: txNumber: ID: NaN AccountNumber: Amount: qNumber: txNumber: ID: NaN
Am I doing the assignment of values on the array correctly? I have included the CashDeposit.js component.