创建 React 项目

创建项目

yarn create react-app my-app
npx create-react-app my-app
npm init react-app my-app

创建typescript项目

在上面的命令的基础上,加--template typescript

环境变量

在 create-react-app 项目中,变量名必须以 REACT_APP_ 开始。

.env 文件中定义变量 REACT_APP_NOT_SECRET_CODE=123

js 中使用

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
render() {
  return (
    <div>
      <small>You are running this application in <b>{process.env.NODE_ENV}</b> mode.</small>
      <form>
        <input type="hidden" defaultValue={process.env.REACT_APP_NOT_SECRET_CODE} />
      </form>
    </div>
  );
}

html 中使用

1
<title>%REACT_APP_WEBSITE_NAME%</title>

判断当前环境 process.env.NODE_ENV !== 'production'

updatedupdated2023-09-242023-09-24