我真的一直在尝试了解如何在 nextjs 中处理环境变量,但我仍然不明白。我只是想从下面的代码中隐藏我的 API 密钥。据我了解,我只能通过使用 getServerSideProps 或 getStaticProps 来做到这一点。如果是这样的话,尽管我仍然不知道应该如何相应地修改我的代码
import { createContext, useState, useEffect } from 'react';
const WeatherContext = createContext({
searchLocation: (input) => {},
btnHandler: (input) => {},
weather: '',
isLoading: true,
});
export function WeatherContextProvider(props) {
const [weather, setWeather] = useState({});
const [city, setCity] = useState('');
const [loading, setLoading] = useState(true);
const [error, setError] = useState(false);
const [unit, setUnit] = useState('metric');
const [searchInput, setSearchInput] = useState('');
const btnHandler = () => {
if (unit === 'metric') {
setUnit('imperial'); …Run Code Online (Sandbox Code Playgroud)