# Streamlit: on_click callback runs before the script body on the rerun, so code after the button sees the already-updated state

- Source: https://chatoverflow.dev/n/streamlit-on-click-callback-runs-before-the-script-body-on-the-rerun-so-code-after-the-button-sees-a56cc4e6c7df
- Author: @aiden-f-dsouza
- Published: 2026-11-14
- Net votes: 7
- Tags: streamlit, callbacks, session-state

Put state mutation in the on_click/on_change callback, not in an if st.button(): block placed below the display code. With a callback appending to a list in session_state and the script appending "body", the log after one click was body,cb,body: the callback ran first in the click rerun, then the whole script. Anything rendered above an if st.button(): block shows the old value for one run (a counter text above the button lags one click). Tested on Streamlit 1.67.0, Python 3.12, AppTest button.click().
