I am creating a stock app that allows the user to search stock prices by calling a Stock API and search any stock and get back its price.
Im storing the prices on a table, however the I want to be able to store the users input into the table as well.
My strategy has been to store the user's search input into an array "symbolArray" and the api information into a 'dataArray."
So heres my issues. I am able to map over the "dataArray" and render it to the table. BUT i need to map over "symbolArray" and render it into the table thats already rendering items from the "data Array."
Heres what I have so far
const Quotes = ()=> {
//I call their states
const [symbolArray, setSymbolArray] = useState([])
const [dataArray, setDataArray] = useState([])
//this function stores, and calls the api data that the user searches for
function getData() {
fetch(url)
.then(res => res.json())
.then(data =>{
dataArray.push(data)
})
}
// this is also activated when the user searchs. their input is pushed to the array of stock ticker symbols
const addingStuff = ()=> {
symbolArray.push(input)
}
return (
<>
{/* here a user searches for stock */}
<div class="input-group">
<input id="searchBar" type="search" class="form-control rounded search-bar" placeholder="Enter Ticker Symbol" aria-label="Search"
aria-describedby="search-addon" value={input} onInput={e => setInput(e.target.value)} />
<button type="button" class="searchButton btn p-2 bg-succ" id="searchButton" onClick={()=> {getData(); addingStuff(); }}>Quote This Stock</button>
</div>
{/* here is my table */}
<table class='table'>
<thead>
<tr>
{/* table headers*/}
<th scope='col'>Symbol</th>
<th scope='col'>Current Price</th>
<th scope='col'>Day High</th>
<th scope='col'>Day Low</th>
<th scope='col'>Price Change</th>
<th scope='col'>Open Price</th>
<th scope='col'>Percentage Change</th>
<th scope='col'>Previous Close</th>
</tr>
</thead>
<tbody>
{/* i call the function that gets and stores the data */}
{getData ?
dataArray.map((stock, index) => {
const {c, d, dp, h, l, o, pc} = stock;
return (
<tr key={index}>
{/* here is the issue where the 2 arrays clash */}
<th scope='row'>{symbolArray.map((symbol, i) => { return i})}</th>
<td>{c}</td>
<td>{d}</td>
<td>{dp}</td>
<td>{h}</td>
<td>{l}</td>
<td>{o}</td>
<td>{pc}</td>
</tr>
)
})
: null }
</>
}
inputparameter inaddingStuffit should beconst addingStuff = input => ...