Skip to content

Updating state in sub-model within Init not possible? #781

Answered by meowgorithm
decentral1se asked this question in Q&A
Discussion options

You must be logged in to vote

Hi! So there are two things you'll need to change for this to work. Firstly, return the child model's Init in the parent's Init (Bubble Tea is very mechanical that way):

func (m mainModel) Init() tea.Cmd {
	return m.subModel.Init()
}

Secondly, Init needs to send a message to Update to append to the buffer:

// The message that contains the content for updating the buffer
type bufferContentMsg string

// Send a message to update the buffer.
func (m subModel) Init() tea.Cmd {
	return bufferContentMsg("A")
}

func (m mainModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
	switch msg := msg.(type) {

	// Update the buffer. You could, of course, also call Update on the sub-model
	// here and han…

Replies: 2 comments

Comment options

You must be logged in to vote
0 replies
Answer selected by decentral1se
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants