President Trump’s presidency has been a boon for states. Thanks to major increases in state tax collections in late 2017 and mid-2018, 41 states have recovered from the Great Recession. State bank ­accounts are in much better shape to weather a new recession, according to a recent analysis by Pew, though challenges remain because of the debt that states accumulated after the downturn.

Yet this is no time for spending complacency.

For local governments, the Barack Obama years — characterized by tax hikes, more extensive regulations on businesses and sluggish economic growth — looked like no other in recent memory. Though tax revenues rebounded quickly after most recessions since World War II, states crawled fitfully out of the 2008-09 downturn. By the end of 2014, fewer than half the states had recouped the revenues they lost in the recession and its aftermath.

Making matters worse, in 2015 and 2016, state tax revenues grew more slowly than in any non-recession year in three decades. As Trump took office, state financial positions were deteriorating, and the country looked to be heading back into a recession at a time when many states were ill prepared to deal with one.

The revenue picture finally started to improve in late 2017, and the recovery has picked up rapidly since then — accelerating especially after federal tax cuts in early 2018. In the third quarter of 2018 alone, state tax revenues rose by a robust 5.7 percent, adding to a string of impressive quarterly gains.

More state bank accounts are flush again. In fiscal year 2018, states added nearly $10 billion to their rainy-day funds, led by huge reserves in the coffers of resource-rich Alaska, Wyoming and Texas.

Still, states face big obstacles to continued recovery. Short of cash, many states spent more than they took in for years, piling up debt or using financial gimmicks to paper over deficits. One recent study found that, stretching back to before the Great Recession, 10 states have consistently spent more than they collected — including New Jersey, Illinois, Connecticut, Massachusetts, Maryland and New York.

One measure of the fiscal stress is that by 2016, seven years into the recovery, 15 states were still overspending. Unfunded liabilities in state pension systems — diverting pension payments is always a tempting tactic to avoid immediate spending cuts — have gone from $360 billion in 2007 to $1.5 trillion today.

In 2007, local governments contributed $74 billion to pensions. Now, under pressure to pay off the debt, governments are pouring in more than $150 billion annually to pension systems, though the problem is worsening.

Rapidly mounting Medicaid costs are also pinching governments. In 2016, Medicaid accounted for more than 17 cents of every dollar in state-generated income, up by nearly five cents since 2000. That number is certain to go up now that the 36 states that expanded Medicaid as part of the Affordable Care Act have had to start chipping in for the cost of extra coverage; Washington had covered the cost of the expansion through 2016.

Some of this fiscal stress will dissipate if the current rally in state revenues keeps going, but warning signs abound. State tax collections fell slightly in the fourth quarter of 2018, largely because high-income taxpayers in some states had rushed to prepay taxes in the last quarter of 2017, ahead of changes in the tax code.

But some high-tax states are looking at a decline because wealthy residents are fleeing to lower-tax places after the passage of Trump’s tax reform, which eliminated breaks for state and local taxes. New York saw personal income tax collections drop by 6.6 percent last year, despite the robust economy and strong collections in some other states.

It’s part of a trend that began even before the Trump legislation and may be speeding up. “New Yorkers were already fleeing for Florida, Arizona and other states that tax and spend far less than we do before this legislation was enacted,” New York state Senate Minority Leader John Flanagan said earlier this year.

Healthy economic gains, including a 3.2 percent growth in gross domestic product in the first quarter of 2019, suggest that there’s enough momentum to maintain recent gains. Let’s hope so, because after years of mediocre revenue growth, many states need more time to get their long-term finances in order.

Steven Malanga is the senior editor of City Journal, from which this column was adapted.