I am attracted to purpose,” Dave Eggers says. People need it, he believes, and so do nations. Much of his fiction has reflected on the loss of an American sense of purpose, the decay of the dream; much of his non-fiction has told the stories of immigrants to the US who have shown the drive and generosity missing from the country as a whole. In person, as in his work, Eggers combines an openness to describing darkness and tragedy with a faith in the essential goodness of “everyday people”. He tells me that “whenever there’s a moment when people are inspired to make the world better, I get interested. I’m super corny that way.”

The most recent of Eggers’s many books is The Lifters, a magic realist tale written for eight- to 12-year-olds, in which the “adults have lost hope, lost their way, and it’s the kids who say: ‘Here’s the task, let’s get to it.’” The setting is a town that used to be proudly defined by what it manufactured, but has been gutted by sadness and loss of identity. Carousel is collapsing in on itself, with sinkholes appearing everywhere; it is up to children – the “lifters” – to go underground and prop up its foundations, and those of other disconsolate places around the world.

The Lifters begins with Gran, a boy new to Carousel and ignored by his classmates; he runs into a brick wall just to prove he exists. The only friend he makes is the enigmatic Catalina Catalan, whom he follows into the woods and one night sees disappear into a hillside. “You went into the hill,” he said. “No I didn’t,” she said … “I saw a handle in your hand … I saw a stairway … And a huge room … It was lit up like a church”. “No it wasn’t.” When Gran eventually becomes a lifter himself, the two take a journey to a convention of tunnel-dwellers deep in the Earth’s mantle and confront a force known as “the Hollows”.

Eggers founded 826 Valencia, a young writers’ centre fronted by a shop selling pirate supplies, in 2002. Photograph: Alamy

Catalina was the first character in his mind, the author says. He wanted to capture her knowledge that “she is solely responsible for holding up everything above ground … I am obsessed with giving kids responsibility. We want to protect them, so we give them less and less to do, but they respond to having a task. I mean, I’m not arguing for child labour, but … ” He has talked of children as “heroes in waiting, they just want to be given the opportunity to prove it”.

In saying this, Eggers is drawing on his experience with “all the kids I’ve met here”: across the road from where we are talking in San Francisco is 826 Valencia, the young writers’ centre he founded in 2002, which is fronted by a shop selling pirate supplies. He asked students at the centre, and its equivalents across the US, to read The Lifters at proof stage: “They are the purest readers, because they want to be entertained, but they will also point out errors, inconsistencies.” He took all their edits. They are also discerning, he says, grasping “the connection straight away between the Hollows and the mood of the town. Children get it at a bone level; they generally know where there is pain in their house, and they speak about it candidly.”

It sometimes seems as though Eggers is waging a one-man war against cynicism: both of the magazines with which he is associated, McSweeney’s and the Believer, were begun as responses to a more acerbic literary culture. Certainly, he is drawn to children’s sense of wonder, enthusiasm and fun. His mother (whose original name was McSweeney) was a “pre-school teacher … brilliant at knowing what kids really felt and liked”, and his teenage years were spent as a camp counsellor and running a sports camp: communicating to kids has been central to his life. Toph, the young brother the 21-year-old Eggers looked after following their parents’ deaths – as recounted in his career-making memoir A Heartbreaking Work of Staggering Genius – joined the camp when he was four. Eggers trained as an illustrator, and it has felt natural “circling back” to write the clutch of children’s books that have followed his work on the screenplay of Maurice Sendak’s Where the Wild Things Are. There is, he continues, “no more fun you can have than writing a pure adventure story for the reader that you were at 10”.

Since his work on Where the Wild Things Are (2009), Eggers has written a clutch of children’s books. Photograph: Warner Bros/Sportsphoto Ltd./Allstar

Eggers has said that, as a child, all he ever wore were grey and brown clothes; that his own parents not once went out in the evening; that his father drank and, like Gran’s, was sometimes out of work: “I can remember feeling responsible for my mother’s happiness.” Also, at school, there was his “ludicrous” name: I was Ham and Eggers,” he has recalled. “Egghead, Egg Beater. Eggs was the nicest way anyone would put it.” But he draws directly on only two personal experiences in his depiction of Gran: the excruciating time he wet himself at school, wearing khakis, and tried to cover it up at lunch by spilling milk over himself; and “the game the boys had in fifth grade when we’d form a circle and take turns to kick each other in the head”.

A very early image Eggers had in writing The Lifters was a “sinkhole in the middle of Gran’s school”. The symbolism is obvious: “This country makes no lasting commitment to education; funding is woefully inadequate.” In the book, teachers simply work around the yawning hole in their building, as in life they adapt to insufficient money and just “reschedule a few classes … this is how we run our public education system. Spit and glue and ScotchTape, and no leadership from DC.”

But before the sinkholes in his imagination were the hills. “I grew up in the flattest part of the country, the midwest,” he says. “And the landscape of my imagination was … stormy, grey, bright green hills.” Walking with his own two children (Eggers is married to the writer Vendela Vida) in just this kind of landscape north of his home in the Bay Area, he used to share a fantasy: “What if you had a handle and you could open up a door that led inside the hillside?” It’s a variation of such classic secret-door children’s fiction as the Narnia books, “The Pied Piper of Hamelin” and Coraline. When a few years ago he spent time in another hilly part of the country, rust-belt Pennsylvania, with its “crooked houses on the slopes, pawn shops and empty storefronts”, the narrative of The Lifters – in which purpose overcomes sadness to restore pride and identity – took shape.

Villainising families from war-torn countries coming here for a better life is not strong. It’s not American

Eggers has long written about the decline of US manufacturing, not least in his novel A Hologram for the King (Tom Hanks starred as the dispossessed hero Alan Clay in the film adaptation), and is careful not to situate The Lifters specifically in the age of Trump. Finishing the story during the runup to the presidential election, however, he “couldn’t help” inserting the detail that one potential leader of Carousel campaigns on an imaginary threat from outsiders (in this case, the outsiders are moose). The opposing candidate runs on a platform of more cash for parks and schools.

The election of Trump is “just absolutely crushing for anyone with an optimistic view of our citizenry,” Eggers says. This is especially the case as his understanding of American values is closely bound up with immigration. “It wasn’t until I moved to California,” in the early 1990s, “that I was able to appreciate how profoundly enriched I am, and we all are, by every new arrival … Our strength comes from our welcome and our identity as a haven and a blank canvas that anyone is free to paint on. And I’m enraged and offended by anyone who makes this into a fearful place. Maybe that’s a patriotic thing … I always picture Trump hiding under a table, because he’s afraid of everything: he’s afraid of Mexicans, Muslims, he’s afraid of change. He’s a deeply fearful human who has never acted courageously in his life.”

“Any time immigrants are villainised, scapegoated or threatened,” he continues, “it’s an outrage. It comes from a cowardly place. You have a privileged 72-year-old white man who is villainising families from war-torn countries who are coming here for a better life – that’s pure ignorance, and it’s irrational and not strong and not American.”

Broad-brushed dystopian thriller … Emma Watson in the 2017 film adaptation of The Circle. Photograph: Francois Duhamel/AP

The day before we talk, Eggers has been organising an event with Valentino Deng, the Sudanese “Lost Boy” who found his way to the US, and whose traumatic experiences he channelled into What Is the What, a book Barack Obama advised every one of his White House aides to read. His second interview-based book to explore how outsiders remake America, Zeitoun, is the story of a Muslim house-painter from the Syrian town of Jableh, who helped the victims of Hurricane Katrina but was arrested as a terrorist. This year, Eggers published The Monk of Mokha, about the entrepreneur Mokhtar Alkhanshali, who narrowly escaped Yemen’s civil war, fleeing in a fishing boat with two cases of coffee. The stories of Abdulrahman Zeitoun and Alkhanshali turned out to be more complicated in real life, but Eggers says he is drawn to people, such as the latter, who are “so driven by an idea, and their ability to realise it, that they attract other humans to their light”.

Social media isolates us. Any time you’re next to an actual human you can find common ground

When dwelling on America’s loss of a clear sense of identity, Eggers often refers to the cancellation of the space shuttle programme. He has always been a space enthusiast and attended the final launch in 2011, an experience he tells me he found “very painful”. “Everyone was crying,” he has said: “The shuttle was such a powerful emblem of a sense of optimism and a can-do spirit.” The space programme finds a place in both A Hologram for the King and the less accomplished Your Fathers, Where Are They? And the Prophets, Do They Live Forever?

The remaining novel in Eggers’s unofficial trilogy about a souring of the American spirit is the biggest seller – The Circle (a film last year). A broad-brushed dystopian thriller about privacy and digital technology, it was often referenced during the recent tribulations of Facebook. Published in 2013, it “was supposed to be set about five years in the future – so about now,” he says. “After it came out, I’d speak at colleges and ask the students: how conflicted are you about your relationship with technology and social media? About half of them would raise their hands. And I would ask: how many of you are suspicious of the motives and ethics of the companies who purvey this technology to you? Maybe a third would raise their hands. And then over time it changed. Perhaps a little before the election but certainly afterwards, 100% raised their hands in answer to each question. The trust in those companies is lost.”

“It reminds me,” he says, “of what happened to the tobacco companies, which I covered as a journalist in the 1990s, where after a point nobody believed anything they said. If you smoked, you knew exactly what you were getting into – all the risks. You knew the companies were evil, but you did it because you were addicted. Well, people are addicted to social media. They know the companies are voraciously profit-driven and completely uninterested in privacy or any rights of the individual. But they carry on using the tools because their friends are using them and there’s nothing better.

“Facebook is still Facebook. It went through a little dip; how many people deleted their accounts? People have a high tolerance for being surveilled. I think it will have to get even more poisonous and destructive before there’s a real change. Social media separates and isolates us … Any time you’re next to an actual human … you can find common ground. Humans in person are kind and giving and generous. But online, you see an inversion of every good quality … We are at our best in person.”

The response of Eggers, who remains an untiring and inspiring literary activist, is to focus on personal stories. He mentions a young Ethiopian-American girl he has just encountered at one of the tutoring centres: “Any time you’re cynical about the world, meet a first-generation kid who writes about what she’s grateful for.” He admits he has found the last couple of years “crushing to a point when I do wake up paralysed sometimes” – and his next novel is “incredibly dark”; he has been wading through “terrible footage and files”. But writing children’s stories has helped. Plus his belief in individual drive and purpose is unwavering. “On this block,” he says, “I could introduce you to nine people whose stories would make interesting books … If you’re listening, the stories are never-ending” •

• The Lifters is published by Scholastic. To order a copy for £11.04 (RRP £12.99) go to guardianbookshop.com or call 0330 333 6846. Free UK p&p over £10, online orders only. Phone orders min p&p of £1.99.