Application React (portail)
De Wiki1000
(Différences entre les versions)
(Page créée avec « Category:Portail Category:Latest ») |
|||
Ligne 1 : | Ligne 1 : | ||
+ | |||
+ | ==Modification pour générer une application relative à index.html == | ||
+ | |||
+ | Dans package.json ajouter la variable homepage = ".' | ||
+ | |||
+ | <pre> | ||
+ | { | ||
+ | "name": "myapp", | ||
+ | "version": "0.1.0", | ||
+ | "private": true, | ||
+ | "homepage": ".", | ||
+ | "dependencies": { | ||
+ | "@testing-library/jest-dom": "^4.2.4", | ||
+ | "@testing-library/react": "^9.5.0", | ||
+ | "@testing-library/user-event": "^7.2.1", | ||
+ | "react": "^16.13.1", | ||
+ | "react-dom": "^16.13.1", | ||
+ | "react-scripts": "3.4.1" | ||
+ | }, | ||
+ | "scripts": { | ||
+ | "start": "react-scripts start", | ||
+ | "build": "react-scripts build", | ||
+ | "test": "react-scripts test", | ||
+ | "eject": "react-scripts eject" | ||
+ | }, | ||
+ | "eslintConfig": { | ||
+ | "extends": "react-app" | ||
+ | }, | ||
+ | "browserslist": { | ||
+ | "production": [ | ||
+ | ">0.2%", | ||
+ | "not dead", | ||
+ | "not op_mini all" | ||
+ | ], | ||
+ | "development": [ | ||
+ | "last 1 chrome version", | ||
+ | "last 1 firefox version", | ||
+ | "last 1 safari version" | ||
+ | ] | ||
+ | } | ||
+ | } | ||
+ | </pre> | ||
+ | |||
+ | == Modification pour copier l'application statique dans le dossier de l'application html de FRP 1000 == | ||
+ | |||
+ | Modifier package.json en ajoutant une section postbuild : | ||
+ | |||
+ | <pre> | ||
+ | { | ||
+ | "name": "myapp", | ||
+ | "version": "0.1.0", | ||
+ | "private": true, | ||
+ | "homepage": ".", | ||
+ | "dependencies": { | ||
+ | "@testing-library/jest-dom": "^4.2.4", | ||
+ | "@testing-library/react": "^9.5.0", | ||
+ | "@testing-library/user-event": "^7.2.1", | ||
+ | "react": "^16.13.1", | ||
+ | "react-dom": "^16.13.1", | ||
+ | "react-scripts": "3.4.1" | ||
+ | }, | ||
+ | "scripts": { | ||
+ | "start": "react-scripts start", | ||
+ | "build": "react-scripts build", | ||
+ | "postbuild": "cp -r -f ./build/* /c/LocalSite900/htmls/testreact", | ||
+ | "test": "react-scripts test", | ||
+ | "eject": "react-scripts eject" | ||
+ | }, | ||
+ | "eslintConfig": { | ||
+ | "extends": "react-app" | ||
+ | }, | ||
+ | "browserslist": { | ||
+ | "production": [ | ||
+ | ">0.2%", | ||
+ | "not dead", | ||
+ | "not op_mini all" | ||
+ | ], | ||
+ | "development": [ | ||
+ | "last 1 chrome version", | ||
+ | "last 1 firefox version", | ||
+ | "last 1 safari version" | ||
+ | ] | ||
+ | } | ||
+ | } | ||
+ | </pre> | ||
+ | |||
+ | == Modification pour gérer des variables d'environnement pour configurer l'application== | ||
+ | |||
+ | Par exemple, pour définir des URLs d'API différentes en production et développement | ||
+ | |||
+ | Créer un fichier ".env.development" et un fichier ".env.production" à la racine de l'application React | ||
+ | |||
+ | <pre> | ||
+ | REACT_APP_NOT_SECRET_CODE=Sage FRP 1000 (development) | ||
+ | </pre> | ||
+ | |||
+ | <pre> | ||
+ | REACT_APP_NOT_SECRET_CODE=Sage FRP 1000 (production) | ||
+ | </pre> | ||
+ | |||
+ | Remarque: les nom de variables doivent commencer par RACT_APP_ | ||
+ | |||
+ | Dans un composant React tester la variable d'environnement : | ||
+ | |||
+ | <pre> | ||
+ | import React from 'react'; | ||
+ | import logo from './logo.svg'; | ||
+ | import './App.css'; | ||
+ | |||
+ | function App() { | ||
+ | return ( | ||
+ | <div className="App"> | ||
+ | <header className="App-header"> | ||
+ | <img src={logo} className="App-logo" alt="logo" /> | ||
+ | <p> | ||
+ | Edit <code>src/App.js</code> and save to reload. | ||
+ | </p> | ||
+ | <a | ||
+ | className="App-link" | ||
+ | href="https://reactjs.org" | ||
+ | target="_blank" | ||
+ | rel="noopener noreferrer" | ||
+ | > | ||
+ | Learn React for {process.env.REACT_APP_NOT_SECRET_CODE} | ||
+ | </a> | ||
+ | </header> | ||
+ | </div> | ||
+ | ); | ||
+ | } | ||
+ | export default App; | ||
+ | </pre> | ||
+ | |||
[[Category:Portail]] | [[Category:Portail]] | ||
[[Category:Latest]] | [[Category:Latest]] |
Version du 4 juillet 2020 à 20:59
Modification pour générer une application relative à index.html
Dans package.json ajouter la variable homepage = ".'
{ "name": "myapp", "version": "0.1.0", "private": true, "homepage": ".", "dependencies": { "@testing-library/jest-dom": "^4.2.4", "@testing-library/react": "^9.5.0", "@testing-library/user-event": "^7.2.1", "react": "^16.13.1", "react-dom": "^16.13.1", "react-scripts": "3.4.1" }, "scripts": { "start": "react-scripts start", "build": "react-scripts build", "test": "react-scripts test", "eject": "react-scripts eject" }, "eslintConfig": { "extends": "react-app" }, "browserslist": { "production": [ ">0.2%", "not dead", "not op_mini all" ], "development": [ "last 1 chrome version", "last 1 firefox version", "last 1 safari version" ] } }
Modification pour copier l'application statique dans le dossier de l'application html de FRP 1000
Modifier package.json en ajoutant une section postbuild :
{ "name": "myapp", "version": "0.1.0", "private": true, "homepage": ".", "dependencies": { "@testing-library/jest-dom": "^4.2.4", "@testing-library/react": "^9.5.0", "@testing-library/user-event": "^7.2.1", "react": "^16.13.1", "react-dom": "^16.13.1", "react-scripts": "3.4.1" }, "scripts": { "start": "react-scripts start", "build": "react-scripts build", "postbuild": "cp -r -f ./build/* /c/LocalSite900/htmls/testreact", "test": "react-scripts test", "eject": "react-scripts eject" }, "eslintConfig": { "extends": "react-app" }, "browserslist": { "production": [ ">0.2%", "not dead", "not op_mini all" ], "development": [ "last 1 chrome version", "last 1 firefox version", "last 1 safari version" ] } }
Modification pour gérer des variables d'environnement pour configurer l'application
Par exemple, pour définir des URLs d'API différentes en production et développement
Créer un fichier ".env.development" et un fichier ".env.production" à la racine de l'application React
REACT_APP_NOT_SECRET_CODE=Sage FRP 1000 (development)
REACT_APP_NOT_SECRET_CODE=Sage FRP 1000 (production)
Remarque: les nom de variables doivent commencer par RACT_APP_
Dans un composant React tester la variable d'environnement :
import React from 'react'; import logo from './logo.svg'; import './App.css'; function App() { return ( <div className="App"> <header className="App-header"> <img src={logo} className="App-logo" alt="logo" /> <p> Edit <code>src/App.js</code> and save to reload. </p> <a className="App-link" href="https://reactjs.org" target="_blank" rel="noopener noreferrer" > Learn React for {process.env.REACT_APP_NOT_SECRET_CODE} </a> </header> </div> ); } export default App;