小编ill*_*ria的帖子

nextjs 中的环境变量

我真的一直在尝试了解如何在 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)

reactjs next.js

4
推荐指数
1
解决办法
3183
查看次数

标签 统计

next.js ×1

reactjs ×1