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

it is receiving the right data, just not setting it

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

4 comments sorted by

View all comments

Show parent comment

2

u/[deleted] Mar 21 '22

thank you!!! I figured it out