When a list item is clicked, the modifyItem() is called to append a new string to the items array.
From the console logs, it seems that the items array had been modified before the push method (*** below) is even called?? The statements in the function are not executed in sequence?
Appreciate some light shed on this.
Thank you!
Console Log Output:
Items BEFORE modification ["1. Click me and check console log", "2. Placeholder", "additonal text"] // ***
Items AFTER modification ["1. Click me and check console log", "2. Placeholder", "additonal text"]
App.jsx
import List from './List'
import "../styles.css";
export default function App()
{
const items = ['1. Click me and check console log', '2. Placeholder']
function modifyItem()
{
console.log('Items BEFORE modification')
console.log(items)
items.push('additonal text')
console.log('Items AFTER modification')
console.log(items)
}
return (
<div className="App">
<ul>
{ items.map((item, index) =>
{
return <List key={index} id={index} modifyItem={modifyItem} text={item} />
})}
</ul>
</div>
);
}
List.jsx
function List(props)
{
function modifyMe()
{
console.log('Start of ModifyMe')
props.modifyItem(props.index)
console.log('End of ModifyMe')
}
return <li onClick={ modifyMe } >{ props.text }</li>
}