r/learnreactjs • • Mar 21 '22
New to react - problems with setting state
I am new to react and I have a simple app, but the state setting function from the useState hook is not working. Please help! Thanks
Here is the code from the parent component
const [info, setInfo] = useState({
name: '',
portfolio: [],
search_results: [],
active_currency: null,
amount: ''
})
const handleSelect = (curr, e) => {
e.preventDefault()
const activeCurrency = info.search_results.find( item => item.id == curr.id)
setInfo({ ...info, active_currency: activeCurrency })
console.log('activeCurrency: ', activeCurrency)
console.log('from setInfo: ', info.active_currency)
setInfo({ ...info, search_results: [] })
showCalculate(true)
}Here is the console.log messages

here is the child component
<div className='currency-list'>
{search_results.map
(curr => (<li key={curr.id} className='currency-list-item'
onClick={(e) => handleSelect(curr, e)}>
<a href='#' data-id={curr.id} className='currency'>
<span>{curr.name}</span>
<span>{curr.currency_symbol}</span>
</a>
</li>))}
</div> From the parent I am passing down handleSelect = {handleSelect}
5
Upvotes
View all comments
Show parent comment
1
u/[deleted] Mar 21 '22
no that was just messy coding I forgot to fix up. I ended up moving them into their own states and then compiling them into an info object at the end. I need to make this function async so that setInfo happens after setPorfolio, but struggling.