Ideally, you get a great carb-heavy meal 3 hours before your race or long ride. This is how long it takes for your body to actually absorb it and fuel up your glycogen stores. Make it easy on your stomach. I eat a weird oatmeal porridge every morning with hemp seed, flax, peanut butter, etc in it. It’s about 30g of protein which helps balance out the carbs from the oats and has plenty of fiber.
What you need for BMX is going to be very different than what you need for a long trail ride, however. BMX is going to be much more fast twitch and not using up your glycogen, but you should still be somewhat fueled for the start. Definitely don’t want to go in hungry, but keep it small before the race.
When I raced track, I regularly did evening races and I just ate a solid lunch, then maybe had a clif bar before a night of racing, then a protein bar after, then a big meal when I got home. Much easier to manage.
With trail riding, it really depends on the length. For a 1-1.5 hour after work ride, I can do it off the lunch from 5 hours before, but I need a bar to eat mid-ride. Longer than that and it’s probably better if I eat a light meal before the ride. As long as I keep it small, it wont upset my stomach.
I struggle a lot to eat during rides beyond maybe a bar or two, so eating a calorie dense meal beforehand is the main way to avoid bonking. And if the ride is over 3 hours, I try to actually stop to eat during the ride to ensure I don’t just take one bite of a bar then put it back and end up super underfueled. If you pack a sandwich, actually plan a spot you’ll stop for 15-20 minutes, drink water, eat your sammy, etc while sitting on a log rather than trying to stuff yourself in a 5 minute break.
If your stomach struggles with this, another option I went with for bikepacking was to get Tailwind or something similar - 200 calorie packets you put in a water bottle. I alternating drinking that and plain water to get a constant flow of calories. I still tried to get a bar in every 10 miles or so, but I knew i couldn’t eat enough while riding on an 8 hour day to make up for it, so i brought 2 tailwinds per day just to keep a constant source fo sugar going (straight up candy/sweets doesnt sit well with me, but i can do a little sugar water).