In the 5th book, like in the show, one of her dragons eats a kid. So she locks away the two that are with her (Drogon has gone off on his own for a bit by this point). At the end of the book she has left Mereen on Drogon's back and is out in this grass plain. She realises that she can't actually remember the name of the kid who died anymore, it's no longer important to her because on some level she doesn't really care about it as much as she thought. She has these visions where she's told by figures from her past that she's forgotten who she really is. A lot of Dany's story has been about her figuring out whether she's this benevolent "Mhysa"/mother figure or is she a "fire and blood" conqueror. Her story in Mereen was about what happens when she actually tries to settle down and rule somewhere rather than just going from place to place conquering them through force. What she finds is that she doesn't enjoy maintaining peace, it's unsatisfying and requires you to make compromises with people you don't like rather than just getting your way. Reading about her time in Mereen is frustrating, something Martin has confirmed he did intentionally, because it feels like Dany is stuck there doing nothing when we want her to be out burning shit up. Like with Dany, we the readers might think we want peace but Dany getting what she wants through force is a lot simpler and more satisfying.
While she's out on that grass plain she sees an illusion of Jorah out on the plain and has this exchange:
"Dragons plant no trees. Remember that. Remember who you are, what you were made to be. Remember your words. "
“Fire and Blood,” Daenerys told the swaying grass.
Dany's not about settling down and doing things for the long term, like planting trees. Much easier to just burn shit and make people bend the knee.
One of the recurring themes in Dany's story is her thinking about "the house with the red door" which is somewhere she grew up and is this symbol for her longing for that sense of home. In the first book she thinks about going "home" to Kings Landing, a place she's never actually been, and pictures it like this.
But it was not the plains Dany saw then. It was King’s Landing and the great Red Keep that Aegon the Conqueror had built. It was Dragonstone where she had been born. In her mind’s eye they burned with a thousand lights, a fire blazing in every window. In her mind’s eye, all the doors were red.
Not exactly hard to figure out the foreshadowing of why there's a fire blazing in every window and all the doors were red.
People have been speculating for years that Dany will end up burning down King's Landing. So it wasn't a huge shock when she did it. The show's problem was that they really didn't build it up like the books did because the show can't portray her inner monologue. Also the show has a track record of sanding off the rough edges of the main characters to remove elements that made them less likeable. Tyrion is the biggest example of this but they did it with Dany too. So when the time came her deciding to burn King's Landing felt like it came out of nowhere.