https://kotlinlang.org logo
#getting-started
Title
# getting-started
a

Alex Goldman

10/26/2023, 9:25 PM
what is the best way to pass state between two composables?
Copy code
//parent composable
fun Foo(listItems: SnapshotStateList<Bar>) {
    val context = LocalContext.current

    val dataStore = Foo(context)
    val scope = rememberCoroutineScope()


    var FooBarState by remember {
        mutableStateOf("")
    }

 //child composable that needs to change on button click would go here
c

Casey Brooks

10/26/2023, 9:30 PM
Hoist the state to a common parent Composable, so that the value can be passed down to all children that need to read/update it
a

Alex Goldman

10/27/2023, 7:28 PM
Still having a ton of trouble but this is tied to a very specific use case. Thanks for sending.
c

Casey Brooks

10/27/2023, 7:29 PM
Can you share an example of what exactly you’re trying to do? The original snippet isn’t very clear
a

Alex Goldman

10/27/2023, 7:39 PM
Sorry, I'm more used to just sharing full code to get advice. Im creating an RSS feed app for practice. There are two textfields one takes a feedpath, and one takes a feedtag. After one is ceated they can be edited. The issue I am having is when I edit a feedpath, it creates a new entry instead of replacing the old one in the data store. This is where I am calling the composable in the parent one. updateOldFeedTag should take care of making sure old data gets updated instead of adding a new entry
Copy code
TagRow(
            tag = tagState,
            updateTag = {
                    newTag -> tagState = newTag
                     Log.d("DEBUG", "Updating tag to: $newTag")
                        },
            feedPath = feedPathState,
            updateFeedPath = { newPath ->
                feedPathState = newPath
                Log.d("DEBUG", "Updating feed to: $newPath")

            },
            listItems = listItems,
            updateOldFeedData = { data ->
                oldFeedData = data
                Log.d("DEBUG", "Updating data to: $data")
            }
this is the state in that composable
Copy code
var oldFeedData by remember { mutableStateOf<FeedData?>(null) }
Then I have some logic in the child composable with an onValueChange and onClick to handle editing an existing item.