React With Django, Not auto refreshing without npm run dev - reactjs

I've built React components in Django, but every time I update my react component I have to run
npm run dev + reload the browser to see the changes.
How can I fix this issue to make react refresh automatically after changes?
I have --watch on my script but still doesn't work
package.json
{
"name": "frontend",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"dev": "webpack --mode development --watch",
"build": "webpack --mode production"
},
"keywords": [],
"author": "",
"license": "ISC",
"devDependencies": {
"#babel/core": "^7.12.3",
"#babel/preset-env": "^7.12.1",
"#babel/preset-react": "^7.12.5",
"babel-loader": "^8.1.0",
"react": "^17.0.1",
"react-dom": "^17.0.1",
"webpack": "^5.4.0",
"webpack-cli": "^4.2.0"
},
"dependencies": {
"#babel/plugin-proposal-class-properties": "^7.12.1",
"#material-ui/core": "^4.11.0",
"#material-ui/icons": "^4.9.1",
"react-router-dom": "^5.2.0"
}
}
index.js
import App from "./components/App";
App.js
import React, {Component} from "react";
import {render} from "react-dom";
import HomePage from "./HomePage";
class App extends Component {
constructor(props) {
super(props);
}
render() {
return (
<div className="center">
<HomePage/>
</div>
);
}
}
export default App;
const appDiv = document.getElementById("app");
render(<App />, appDiv);
Homepage.js
import React, {Component} from 'react';
class Homepage extends Component {
constructor(props) {
super(props);
}
render() {
return (
<div>
test 1
</div>
);
}
}
export default Homepage;

The reason is very obvious, you have integrated React in your Django application manually without CRA(create-react-app), and your Django page is isolated from your React application, Django will only detect changes in your .py files.
But there are some workaround for this, either you can use this package for your Django app -
https://github.com/fabiogibson/django-livesync
Or you can also try browsersync, which is a little complicated to integrate with Django, but possible.

A simple solution is to use the two terminals simultaneously - one for Python manage.py runserver and the other for npm run dev.

Related

Getting invalid hook call warning when using react-bootstrap

I'm having issues with react-bootstrap, specifically whenever I try and use a react-bootstrap tag, like <Row></Row>, I'm getting the "Invalid hook call" error on my app.
I've run npm ls and these are the packages/versions I currently have installed:
boostrap#4.6.0
react-bootstrap#1.6.1
react-dom#17.0.2
react#17.0.2
I've also followed the advice in the React documentation on this error and added logs to see if I've got duplicate copies of React, but the test is returning 'true' in the console.
Here's the package.json file in my project:
{
"name": "kanvaswebappr",
"version": "0.1.0",
"private": true,
"dependencies": {
"bootstrap": "^4.1.3",
"jquery": "^3.5.1",
"merge": "^1.2.1",
"oidc-client": "^1.9.0",
"react-router-bootstrap": "^0.25.0",
"react-router-dom": "^5.1.2",
"react-scripts": "^3.4.4",
"reactstrap": "^8.4.1",
"rimraf": "^2.6.2"
},
"devDependencies": {
"ajv": "^6.9.1",
"cross-env": "^5.2.0",
"typescript": "^3.7.5",
"eslint": "^6.8.0",
"eslint-config-react-app": "^5.2.0",
"eslint-plugin-flowtype": "^4.6.0",
"eslint-plugin-import": "^2.20.1",
"eslint-plugin-jsx-a11y": "^6.2.3",
"eslint-plugin-react": "^7.18.3",
"nan": "^2.14.1"
},
"peerDependencies": {
"react": "^16.0.0",
"react-dom": "^16.0.0"
},
"eslintConfig": {
"extends": "react-app"
},
"scripts": {
"start": "rimraf ./build && react-scripts start",
"build": "react-scripts build",
"test": "cross-env CI=true react-scripts test --env=jsdom",
"eject": "react-scripts eject",
"lint": "eslint ./src/"
},
"browserslist": {
"production": [
">0.2%",
"not dead",
"not op_mini all"
],
"development": [
"last 1 chrome version",
"last 1 firefox version",
"last 1 safari version"
]
}
}
As you can see, I've moved react and reactdom into peeDependencies and then run npm install, but that hasn't helped either.
My app is currently super basic because I'm just starting out/learning.
My index.js file:
import React from 'react';
import ReactDOM from 'react-dom';
import { BrowserRouter } from 'react-router-dom';
import App from './containers/App';
import registerServiceWorker from './registerServiceWorker';
import 'bootstrap/dist/css/bootstrap.css';
const baseUrl = document.getElementsByTagName('base')[0].getAttribute('href');
const rootElement = document.getElementById('root');
ReactDOM.render(
<BrowserRouter basename={baseUrl}>
<App />
</BrowserRouter>,
rootElement);
My App.js file:
import React, { Component } from 'react';
import './App.css';
import Layout from '../components/Layout/Layout';
class App extends Component {
render() {
return (
<Layout>
<strong>This content is going to be rendered as the props.children inside the Layout component.</strong>
</Layout>
);
}
}
export default App;
My Layout.js file:
import React from 'react';
import Alert from 'react-bootstrap/Alert';
const layout = (props) => {
return (
<div>
<div>
This is the place for the navigation component.
</div>
<Alert>
This is an alert—check it out!
</Alert>
<main>
{props.children}
</main>
</div>
)
}
export default layout;
If anyone is able to help I would be incredibly grateful!
I fixed it myself after a bit more messing around this morning. Posting this here in case it's of any use to anyone else.
I'm building my React app as part of an asp.net solution and I had a node_modules folder at both solution level and in my actual React app/project. I'm very new to all this so I think I'd managed to install packages in both places and they were out of sync in terms of what was installed and version numbers. I ran npm update at both levels to bring everything up to date and then I ran npm link MyApp/node-modules/react from the solution folder just to make sure that React wasn't being duplicated (although I don't think it was).
All seems to be working now.
solved it by going into node modules folder to check the versions react and react-dom installations. then I changed those that were behind. actually i deleted the node modules folder.

Trying to Resolve Issue "Attempted import error: 'BrowerRouter' is not exported from 'react-router-dom' (imported as 'Router')."

learning some React been following a tutorial and I keep running into this particular issue. I've shut down the deve server and restarted VSCode which seemed to resolve the issue for others.
Here's my App.js
import React, { Fragment } from "react";
import { BrowerRouter as Router, Route, Switch } from "react-router-dom";
import Navbar from "./components/layout/Navbar";
import Landing from "./components/layout/Landing";
import "./App.css";
const App = () => {
<Router>
<Fragment>
<Navbar />
<Route exact path="/" component={Landing} />
</Fragment>
</Router>;
};
export default App;
and here is my package.json
{
"name": "devconnector",
"version": "1.0.0",
"description": "AM doing BT's React Course",
"main": "server.js",
"scripts": {
"start": "node server",
"server": "nodemon server",
"client": "npm start --prefix client",
"dev": "concurrently \"npm run server\" \"npm run client\""
},
"author": "Andrew M.",
"license": "MIT",
"dependencies": {
"bcryptjs": "^2.4.3",
"config": "^3.3.1",
"express": "^4.17.1",
"express-validator": "^6.6.1",
"gravatar": "^1.8.1",
"jsonwebtoken": "^8.5.1",
"mongoose": "^5.9.27",
"react-router-dom": "^5.2.0",
"request": "^2.88.2"
},
"devDependencies": {
"concurrently": "^5.2.0",
"nodemon": "^2.0.4"
},
"proxy": "http://localhost:5000"
}
If you've got any advice at all please help
It's a typo. You typed 'BrowerRouter' when it's 'BrowserRouter'

Gatsby - Build - WebpackError : Invariant failed

I'm having troubles building my gatsby project.
Development is working fine, but when i try to build, I get an error :
ERROR #95313
Building static HTML failed for path "/Components/Layout/Footer/"
See our docs page for more info on this error: https://gatsby.dev/debug-html
7 |
8 | if (isProduction) {
9 | throw new Error(prefix);
| ^
10 | } else {
11 | throw new Error(prefix + ": " + (message || ''));
12 | }
WebpackError: Invariant failed
- tiny-invariant.esm.js:9 invariant
[front-gatsby]/[tiny-invariant]/dist/tiny-invariant.esm.js:9:1
- react-router-dom.js:172 Object.children
[front-gatsby]/[react-router-dom]/esm/react-router-dom.js:172:132
- static-entry.js:240 Module.../../../../../front-gatsby/.cache/static-entry.js.__webpack_exports__.default
/front-gatsby/.cache/static-entry.js:240:18
- api-runner-ssr.js:19 MappingPromiseArray.PromiseArray._iterate
/front-gatsby/.cache/api-runner-ssr.js:19:1
I have no idea where to start debugging this. My website is pretty simple with some static pages and redux.
Below is my code.
I have basically a static website using redux and react router dom to navigate.
I had previously an error when building : "couldn't find the store" which is why I had to create both gatsby-browser and gatsby-ssr to enclose the Provider :
index.js:
import React, { Component } from 'react';
import './App.css';
import Navigation from './Components/Navigation'
import { library } from '#fortawesome/fontawesome-svg-core'
import { faStroopwafel, faStar, faCircle, faCheckCircle,faTimesCircle} from '#fortawesome/free-solid-svg-icons'
import 'bootstrap/dist/css/bootstrap.min.css';
class App extends Component {
render() {
library.add(faStroopwafel, faStar, faCircle, faCheckCircle, faTimesCircle)
return (
<div className="App">
<Navigation/>
</div>
);
}
}
export default App;
ReduxWrapper :
import React, { Component } from 'react';
import shop from './reducers/shop.reducer'; // importation du shop
import erreur from './reducers/erreur.reducer'; // importation du erreur
import panier from './reducers/panier.reducer'; // importation du erreur
import {Provider} from 'react-redux'; // importation du provider
import createEngine from 'redux-storage-engine-localstorage';
import {createStore, combineReducers, applyMiddleware} from 'redux'; // importation du reduceur
import * as storage from 'redux-storage'; // storage
const reducer = storage.reducer(combineReducers({shop, erreur,panier}));
const engine = createEngine('my-save-key');
const middleware = storage.createMiddleware(engine);
const createStoreWithMiddleware = applyMiddleware(middleware)(createStore);
const store = createStoreWithMiddleware(reducer);
const load = storage.createLoader(engine);
load(store);
export default ({ element }) => (
<Provider store={store}>{element}</Provider>
);
gatsby-browser & gatsby-ssr (identicals)
export { default as wrapRootElement } from './src/pages/ReduxWrapper';
My Package.json :
{
"name": "gatsby-starter-hello-world",
"private": true,
"description": "",
"version": "0.1.0",
"license": "MIT",
"scripts": {
"build": "gatsby build",
"develop": "gatsby develop",
"format": "prettier --write \"**/*.{js,jsx,json,md}\"",
"start": "npm run develop",
"serve": "gatsby serve",
"test": "echo \"Write tests! -> https://gatsby.dev/unit-testing \""
},
"dependencies": {
"#feathersjs/feathers": "^4.3.0-pre.1",
"#feathersjs/rest-client": "^4.3.0-pre.1",
"#fortawesome/fontawesome-svg-core": "^1.2.18",
"#fortawesome/free-solid-svg-icons": "^5.8.2",
"#fortawesome/react-fontawesome": "^0.1.4",
"bootstrap": "^4.3.1",
"emailjs-com": "^2.4.0",
"express": "^4.17.0",
"express-favicon": "^2.0.1",
"fs": "0.0.1-security",
"gatsby": "^2.15.28",
"gatsby-plugin-mailchimp": "^5.1.2",
"gatsby-plugin-react-helmet": "^3.1.10",
"gatsby-plugin-stripe": "^1.2.3",
"react": "^16.10.1",
"react-bootstrap": "^1.0.0-beta.10",
"react-dom": "^16.10.1",
"react-helmet": "^5.2.1",
"react-redux": "^7.0.3",
"react-responsive-carousel": "^3.1.49",
"react-router-dom": "^5.0.0",
"react-router-hash-link": "^1.2.1",
"react-router-hash-link-offset": "^1.0.1",
"react-scripts": "2.1.8",
"react-scroll-to-component": "^1.0.2",
"react-stripe-checkout": "^2.6.3",
"react-stripe-elements": "^4.0.0",
"reactstrap": "^7.1.0",
"redux": "^4.0.1",
"redux-storage": "^4.1.2",
"redux-storage-engine-localstorage": "^1.1.4",
"uuid": "^3.3.3"
},
"devDependencies": {
"prettier": "^1.18.2"
},
"repository": {
"type": "git",
"url": "https://github.com/gatsbyjs/gatsby-starter-hello-world"
},
"bugs": {
"url": "https://github.com/gatsbyjs/gatsby/issues"
},
"eslintConfig": {
"extends": "react-app"
},
"browserslist": [
">0.2%",
"not dead",
"not ie <= 11",
"not op_mini all"
]
}
This doesn't work when running "gatsby build", it stops at the step "Building static HTML for pages" and the error above is shown
I'd bet the problem is your use of redux-storage. Gatsby can't access any browser APIs during build, so you'll need to wrap a conditional check to make sure localstorage is available (per the html debugging page that has already been suggested).
I'm not familiar with redux usage in gatsby, but I would rewrite your redux wrapper to be closer to the official example, and put the redux-storage setup in a if (typeof window !== 'undefined') block

Redux + React Native + Expo - In development environment, component not rendered after updating state, but it works in production

I encounter a problem with, I think, Expo environment
When my Redux State is updated, my components are not rendered, whereas in Production mode, everything works fine
I do not print my code because it's surely not related to code, but rather than Expo environnement. Some others developers encountered that problem, but I still not found a working solution ..
It's just annoying to code in Prod' env rather than Dev' / Local env
Here is my package.json
{
"main": "node_modules/expo/AppEntry.js",
"scripts": {
"start": "expo start",
"android": "expo start --android",
"ios": "expo start --ios",
"eject": "expo eject"
},
"dependencies": {
"expo": "^32.0.0",
"native-base": "^2.12.1",
"react": "^16.5.1",
"react-native": "https://github.com/expo/react-native/archive/sdk-32.0.0.tar.gz",
"react-navigation": "^3.9.1",
"react-redux": "^6.0.0",
"redux": "^4.0.1"
},
"devDependencies": {
"babel-preset-expo": "^5.0.0",
"schedule": "^0.4.0"
},
"private": true
}
If anyone has an idea about how to fix it, it would be great
If you require more information, don't hesitate to ask
EDIT : Here is my app.js
import React from 'react'
import { View, StyleSheet } from 'react-native'
import Navigation from './Navigation/Navigation'
import { Provider } from 'react-redux'
import Store from './Store/configureStore'
class App extends React.Component {
render() {
return (
<Provider store={Store} >
<Navigation />
</Provider>
);
}
}
export default App
Here is another Git post relative to this subject : https://github.com/expo/expo/issues/3859#issuecomment-490979505

Unknown error in Reactjs

I'm trying to learn how to use React and I'm trying to build a todo app. Right now my index.js file is as such:
var React = require('react');
var ReactDom = require('react-dom');
//create component
var TodoComponent = React.createClass({
render: function(){
return(
<h1>Hello!</h1>
);
}
});
//put component into html page
ReactDom.render(<TodoComponent/>, document.getElementById('todo-wrapper'))
but in my terminal I get this error:
ERROR in ./~/react-dom/lib/ReactMount.js
Module not found: Error: Cannot resolve module 'react/lib/React' in /Users/tylerbray/Documents/Side Projects/Ninja React/react-playlist/node_modules/react-dom/lib
# ./~/react-dom/lib/ReactMount.js 15:12-38
here is my json file:
{
"name": "react-playlist",
"version": "1.0.0",
"description": "A simple react to-do list",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "npm run build",
"build": "webpack -d && webpack-dev-server --content-base src/ --inline --hot --port 1234"
},
"repository": {
"type": "git",
"url": "git+https://github.com/iamshaunjp/react-playlist.git"
},
"keywords": [
"react"
],
"author": "The Net Ninja",
"license": "MIT",
"bugs": {
"url": "https://github.com/iamshaunjp/react-playlist/issues"
},
"homepage": "https://github.com/iamshaunjp/react-playlist#readme",
"dependencies": {
"react": "^16.2.0",
"react-dom": "^15.6.2"
},
"devDependencies": {
"babel-core": "^6.26.0",
"babel-loader": "^6.4.1",
"babel-preset-env": "^1.6.1",
"babel-preset-es2015": "^6.24.1",
"babel-preset-react": "^6.24.1",
"webpack": "^1.15.0",
"webpack-dev-server": "^1.16.5"
}
}
I'm not sure what to make of it and I was hoping someone here could point me in the right direction. Repo can be found here: https://github.com/Brayheart/react-todo
var React = require('require');
should be:
var React = require('react');
Problem is that you are using "react-dom": "^15.6.2" which is creating an issue. Packaging has changed after react 16, There is no react/lib/* and react-dom/lib/* anymore (check docs for more details: https://reactjs.org/blog/2017/09/26/react-v16.0.html#packaging).
To fix an issue
update "react-dom" to "^16.2.0".
Remove node_modules
Run "npm install"
Then you also need to change your "index.js" as you have upgraded react version. "React.createClass" is not a part of core package anymore it is extracted to separate package "create-react-class" (for more details check docs: https://reactjs.org/blog/2017/09/26/react-v16.0.html#packaging), So i have modified it to use React.component.
import React from 'react';
import ReactDom from 'react-dom';
//create component
class TodoComponent extends React.Component {
render() {
return(
<h1>Hello!</h1>
);
}
}
//put component into html page
ReactDom.render(<TodoComponent/>, document.getElementById('todo-
wrapper'))
run "npm start"
Opened app using url http://localhost:1234
it should work :)

Resources