Cannot read properties of undefined (reading 'setIn')

Hello,

I’m building a simple application and want to show or hide the contents of a container based on a flag in the session state. The main layout looks like this:

main = st.container()
main.title(title)
body = main.container()
code_container = main.container()
state = main.container()

There is a button which has a method to set the state. When I attempt to operate on that state in code, however, this error is thrown when I click the button.

with code_container:
    if st.session_state.showing_code is True:
        st.write("hi")
    else:
        st.empty()

What am I missing? Thanks for taking the time to consider this - really appreciate it.

This is Streamlit, version 1.10.0

Ah, I figured this out. I was creating content of the container above it later down the line, so the previous container which should have appeared where I wanted to show the code didn’t have any content in it. I fixed it by adding these lines. I did one above the state for good measure, as well, though not strictly necessary in my app right now.

main = st.container()
main.title(title)
body = main.container()
body.empty()  # +++
code_container = main.container()
code_container.empty()  # +++
state = main.container()
1 Like

I was having the same issue and this actually worked. However just thinking about the logic behind this. The container has just been created and has nothing dropped in yet, so what is the reason for calling empty()? Happy it worked though. Thanks for sharing this.