Hi there I recently started following a crash course of Django Rest Framework + React but I came across a problem with my React code. The jsx I write in my App.js doesn't show in the browser. Here is the code:
import React, { Component } from 'react';
import ReactDOM from 'react-dom';
import Header from './layout/Header';
import Dashboard from './leads/Dashboard';
import { Provider } from 'react-redux';
import store from '../store';
class App extends Component {
render() {
return (
<Provider store={store}>
<Header />
<div>
<Dashboard />
</div>
</Provider>
)
}
}
ReactDOM.render(<App />, document.getElementById("app"))
He are my Header.js, and Dashboard.js files(in case it helps)
import React, { Component } from 'react';
export class Header extends Component {
render() {
return (
<div>
<nav className="navbar navbar-expand-sm navbar-light bg-light">
<button className="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarTogglerDemo01" aria-controls="navbarTogglerDemo01" aria-expanded="false" aria-label="Toggle navigation">
<span className="navbar-toggler-icon"></span>
</button>
<div className="collapse navbar-collapse" id="navbarTogglerDemo01">
<a className="navbar-brand" href="#">Django + React</a>
<ul className="navbar-nav mr-auto mt-2 mt-lg-0">
</ul>
</div>
</nav>
</div>
)
}
}
export default Header;
import React from 'react';
import Form from './Form';
import Leads from './Leads';
export default function Dashboard() {
return (
<div>
<Form />
<Leads />
</div>
);
}
In case you need any extra information please tell me. Can anyone please help me solve this problem?? If so please tell me and thank you in advance.
Edit: Here is my package.json file
{
"name": "DjangoReact",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"dev": "webpack --mode development --watch ./backend/frontend/src/index.js --output-path ./backend/frontend/static/frontend",
"build": "webpack --mode production ./backend/frontend/src/index.js --output-path ./backend/frontend/static/frontend/main.js"
},
"keywords": [],
"author": "",
"license": "ISC",
"devDependencies": {
"#babel/core": "^7.12.7",
"#babel/preset-env": "^7.12.7",
"#babel/preset-react": "^7.12.7",
"babel-loader": "^8.2.1",
"babel-plugin-transform-class-properties": "^6.24.1",
"webpack": "^5.6.0",
"webpack-cli": "^4.2.0"
},
"dependencies": {
"prop-types": "^15.7.2",
"react": "^17.0.1",
"react-dom": "^17.0.1",
"react-react": "^1.2.0",
"react-redux": "^7.2.2",
"react-thunk": "^1.0.0",
"redux": "^4.0.5",
"redux-devtools-extension": "^2.13.8",
"redux-thunk": "^2.3.0"
}
}
You will not see jsx files in the browser. The code from jsx files is transformed into html+js code and displayed in the web browser.
Before going to write your own code, please make sure that you have node.js installed and that you can run React template code:
# create the React boilerplate project
npx create-react-app frontend
# go to the fronted project directory
cd frontend
# run React built-in development server
npm start
After the above commands, you should see a default web app at http://localhost:3000.
You can check details in my article: Starting SaaS with Django and React
If you are able to see the default application then you can start to add your code. Here you have the second part of my article: React Routing and Components for Signup and Login
Related
I am trying to preload/prefetch an image so that I don't have to be connected to the internet at the exact moment to display the image. It's for a "you are offline" page in my app.
My app is written in React and I am using webpack for my bundler.
I was reading online that the webpack magic comments feature might be able to do that.
https://medium.com/webpack/link-rel-prefetch-preload-in-webpack-51a52358f84c
It's not working for me.
The image that I am trying to preload/prefetch does not show and there is no <link rel tag added to the DOM.
Checkout the demo repository here:
https://github.com/aubreyquinn/preload-webpack-demo/tree/main
package.json:
"devDependencies": {
"#babel/core": "^7.19.6",
"#babel/preset-env": "^7.19.4",
"#babel/preset-react": "^7.18.6",
"babel-loader": "^8.2.5",
"css-loader": "^6.7.1",
"file-loader": "^6.2.0",
"html-webpack-plugin": "^5.5.0",
"style-loader": "^3.3.1",
"webpack": "^5.74.0",
"webpack-cli": "^4.10.0",
"webpack-dev-server": "^4.11.1"
},
"dependencies": {
"react": "^18.2.0",
"react-dom": "^18.2.0"
}
You have to use the useState hook to trigger the re-render, The webpack prefetching is working fine but react is not re-rendering since it is unaware of such loading.
import React, { useEffect, useState } from "react";
import Flowers from "../assets/flowers.jpg";
// Home component
const Home = () => {
const [lilies, setLilies] = useState(null);
useEffect(() => {
import(/* webpackPrefetch: true */ "../assets/lilies.jpg").then((res) => {
console.log(res);
setLilies(res.default);
});
}, []);
return (
<>
<div>React Application</div>
<img src={Flowers} height={200} />
<img src={lilies} height={200} />
</>
);
};
export default Home;
Try the above code out, it's working just fine.
I'm trying to type new values after instantiating Algolia's SearchBox, but it doesn't change the input value at all. I've recreated the NextJs boilerplate and implemented it 5 times, but the value never changes. What could it be?
In short: when I type something in the input, the value just doesn't update
Github:
https://github.com/Arthur-Maskalenkas/bug-report
sandbox:
https://codesandbox.io/p/github/Arthur-Maskalenkas/bug-report/csb-zf1foq/draft/happy-smoke?file=%2FREADME.md
my currently index.tsx is
import type { NextPage } from 'next'
import algoliasearch from 'algoliasearch/lite';
import {
InstantSearch,
SearchBox,
Pagination,
Configure,
} from 'react-instantsearch-dom';
const Home: NextPage = () => {
const searchClient = algoliasearch(
'B1G2GM9NG0',
'aadef574be1f9252bb48d4ea09b5cfe5'
);
return (
<div className="ais-InstantSearch">
<h1>React InstantSearch e-commerce demos</h1>
<InstantSearch searchClient={searchClient} indexName="demo_ecommerce">
<Configure hitsPerPage={8}/>
<div className="search-panel">
<div className="search-panel__filters">
</div>
<div className="search-panel__results">
<SearchBox
className="searchbox"
translations={{
placeholder: ''
}}
/>
<div className="pagination">
<Pagination/>
</div>
</div>
</div>
</InstantSearch>
</div>
)
}
export default Home
my package
{
"name": "teste2",
"version": "0.1.0",
"private": true,
"scripts": {
"dev": "next dev",
"build": "next build",
"start": "next start",
"lint": "next lint"
},
"dependencies": {
"algoliasearch": "^4.14.2",
"next": "12.3.1",
"react": "18.2.0",
"react-dom": "18.2.0",
"react-instantsearch-dom": "^6.33.0"
},
"devDependencies": {
"#types/node": "18.7.20",
"#types/react": "18.0.21",
"#types/react-dom": "18.0.6",
"#types/react-instantsearch-dom": "^6.12.3",
"eslint": "8.24.0",
"eslint-config-next": "12.3.1",
"typescript": "4.8.3"
}
}
There is a known issue with React 18.x strict mode and older InstantSearch libraries. The resolution is to set reactStrictMode: false in next.config.js (not recommended), downgrade to React 17, or move to the newer react-instantsearch-hooks library as documented here: https://github.com/algolia/react-instantsearch/issues/3634
i am trying to use simple redux form in my react login page ,but when i try the console out values in my email and password field they are allways returned undefined.
This is my signin.js
import React, {Component} from 'react';
import { reduxForm } from 'redux-form';
class Signin extends Component {
handleFormSubmit({ email, password }){
console.log(email,password);
console.log(this.props.email);
}
render(){
const { fields: { email, password }, handleSubmit} = this.props;
return(
<form onSubmit={handleSubmit(this.handleFormSubmit.bind(this))} >
<fieldset className="form-group" >
<label>Email:</label>
<input {...email} className="form-control" component="input" />
{console.log(email)}
</fieldset>
<fieldset className="form-group" >
<label>password:</label>
<input {...password} className="form-control" />
</fieldset>
<button action="submit" className="btn btn-primary"> sign in</button>
</form>
);
}
}
export default reduxForm({
form: 'signin',
fields: ['email','password']
})(Signin);
this my index.js in reducer
import { combineReducers } from 'redux';
import { reducer as form } from 'redux-form';
const rootReducer = combineReducers({
form
});
export default rootReducer;
this is my package.json
{
"name": "redux-simple-starter",
"version": "1.0.0",
"description": "Simple starter package for Redux with React and Babel support",
"main": "index.js",
"repository": "git#github.com:StephenGrider/ReduxSimpleStarter.git",
"scripts": {
"start": "node ./node_modules/webpack-dev-server/bin/webpack-dev-server.js",
"test": "mocha --compilers js:babel-core/register --require ./test/test_helper.js --recursive ./test",
"test:watch": "npm run test -- --watch"
},
"author": "",
"license": "ISC",
"devDependencies": {
"babel-core": "^6.2.1",
"babel-loader": "^6.2.0",
"babel-preset-es2015": "^6.1.18",
"babel-preset-react": "^6.1.18",
"chai": "^3.5.0",
"chai-jquery": "^2.0.0",
"jquery": "^2.2.1",
"jsdom": "^8.1.0",
"mocha": "^2.4.5",
"react-addons-test-utils": "^0.14.7",
"webpack": "^1.12.9",
"webpack-dev-server": "^1.14.0"
},
"dependencies": {
"babel-preset-stage-1": "^6.1.18",
"lodash": "^3.10.1",
"react": "^0.14.3",
"react-dom": "^0.14.3",
"react-redux": "4.3.0",
"react-router": "^2.0.1",
"redux": "^3.0.4",
"redux-form": "^7.3.0"
}
}
i have done all the searching but still not able to find solution for this weird problem.
my input values always return undefined .
I did not use FieldSet yet, but you have to define for redux-form the name of each field. I used Field components it has name property. With FieldSet it seems to me you have to use the model property to define an identifier for redux-form.
The issue is with the reactstrap cards: https://reactstrap.github.io/components/card/
Using reactstrap components like Card, etc is not working.
Component: Version I
import React, { Component } from 'react';
import { Card, Button, CardImg, CardTitle, CardText, CardDeck, CardSubtitle, CardBody } from 'reactstrap';
class MoviesIndex extends Component {
render() {
return (
<CardDeck>
<Card>
<CardImg top width="100%" src="" alt="Movie Poster" />
</Card>
</CardDeck>
);
}
}
export default MoviesIndex;
Output: *It works fine without any errors.
But when I try to use the rest of the components from reactstrap. It throws errors on console.
Component: Version II
import React, { Component } from 'react';
import { Card, Button, CardImg, CardTitle, CardText, CardDeck, CardSubtitle, CardBody } from 'reactstrap';
class MoviesIndex extends Component {
render() {
return (
<CardDeck>
<Card>
<CardImg top width="100%" src="" alt="Movie Poster" />
<CardBody>
<CardTitle>Card title</CardTitle>
<CardSubtitle>Card subtitle</CardSubtitle>
<CardText>This is a wider card with supporting text below as a natural lead-in to additional content. This content is a little bit longer.</CardText>
<Button>Button</Button>
</CardBody>
</Card>
</CardDeck>
);
}
}
export default MoviesIndex;
Output:
package.json
{
"name": "client",
"version": "1.0.0",
"main": "index.js",
"scripts": {
"start": "node ./node_modules/webpack-dev-server/bin/webpack-dev-server.js",
"test": "mocha --compilers js:babel-core/register --require ./test/test_helper.js --recursive ./test",
"test:watch": "npm run test -- --watch"
},
"devDependencies": {
"babel-core": "^6.2.1",
"babel-loader": "^6.2.0",
"babel-preset-es2015": "^6.1.18",
"babel-preset-react": "^6.1.18",
"chai": "^3.5.0",
"chai-jquery": "^2.0.0",
"jquery": "^2.2.1",
"jsdom": "^8.1.0",
"mocha": "^2.4.5",
"react-addons-test-utils": "^0.14.7",
"webpack": "^1.12.9",
"webpack-dev-server": "^1.14.0"
},
"dependencies": {
"axios": "^0.17.1",
"babel-preset-stage-1": "^6.1.18",
"lodash": "^3.10.1",
"react": "^16.2.0",
"react-dom": "^16.2.0",
"react-redux": "^5.0.6",
"react-router": "^4.2.0",
"react-transition-group": "^2.2.1",
"reactstrap": "^4.8.0",
"redux": "^3.7.2",
"redux-promise": "^0.5.3"
}
}
I am not able to debug this issue. Please help! TIA.
The components such as CardBody, etc are not available in v4.8.0.
Upgrading to latest release (reactstrap v5.0.0-alpha.4) resolves this issue!
npm install --save reactstrap#5.0.0-alpha.4
Refer to the issue that I created on reactstrap#github for more details:
https://github.com/reactstrap/reactstrap/issues/730
This looks like a typo here:
renderMovies() {
return _.map(this.props.movies, movie => {
Missing )
Try
_.map(this.props.movies, movie) => {
If that doesn't do it, put console.log() after everything to see where it becomes undefined.
This type of error usually results from something not being exported correctly or something being null/undefined due to data missing, etc.
This is a good opportunity for me to recommend using ES Lint, so you can gain extra help 24/7 from the passive display of errors. Check it out if you aren't using it. It's absolutely worth looking into and using.
I won't recommend any specific linting configs in here. That is out of scope of this help. ES Lint will underline code errors such as the missing ) with a red underline, so you will experience less of this kind of thing :)
I am still new to react and am trying to use react and reactstrap for an internal project at work. I have spent several hours trying to the get the NavDropdown component to work. I have tried porting the example code from https://reactstrap.github.io/components/dropdowns/ into my project to just test it out and built my own nav taking cues from this example https://codepen.io/eddywashere/pen/KgjQay?editors=0010
However, I cannot get the Dropdown menu to open.
When I inspect the element I can see the NavDropdown and its child elements in the inspector. When I click on the DropDown element I can see that its state.isOpen toggles to true. However- the dropdown menu doesn't actually show, it remains closed.
I have no console.log errors and am really mystified about what I am doing wrong. Would love to learn my mistake on this one so we can use the library.
Here is the header component I am trying to use the NavDropDown in:
import React from 'react';
import { Navbar, Nav, NavDropdown, DropdownToggle, DropdownMenu, DropdownItem} from 'reactstrap';
class Header extends React.Component {
constructor(props) {
super(props);
this.state = {
dropdown: false
};
this.dropdownToggle = this.dropdownToggle.bind(this);
}
dropdownToggle() {
this.setState({
dropdown: !this.state.dropdown
});
}
render() {
return (
<header>
<Navbar color="light">
<Nav navbar>
<NavDropdown isOpen={this.state.dropdown} toggle={this.dropdownToggle}>
<DropdownToggle color="primary" nav caret>
Dropdown
</DropdownToggle>
<DropdownMenu>
<DropdownItem>Header</DropdownItem>
<DropdownItem>Action</DropdownItem>
<DropdownItem>Another Action</DropdownItem>
<DropdownItem>Another Action</DropdownItem>
</DropdownMenu>
</NavDropdown>
</Nav>
</Navbar>
</header>
);
}
}
export default Header
Here is App.js file I import the header into:
import React from 'react';
import Header from './shared/Header';
class App extends React.Component {
render() {
return (
<div>
<Header/>
</div>
);
}
}
export default App;`
Here is my index.js
import React from 'react';
import ReactDOM from 'react-dom';
import { BrowserRouter } from 'react-router-dom'
import App from './comp/App';
import registerServiceWorker from './registerServiceWorker';
import 'bootstrap/dist/css/bootstrap.css';
import './Index.css';
ReactDOM.render(
<BrowserRouter><App /></BrowserRouter>,
document.getElementById('root')
);
registerServiceWorker();
I used create-react-app to start the project and here is my package.json
{
"name": "estimator",
"version": "0.1.0",
"private": true,
"dependencies": {
"bootstrap": "^4.0.0-alpha.6",
"node-sass-chokidar": "0.0.3",
"prop-types": "^15.5.10",
"react": "^15.6.1",
"react-addons-css-transition-group": "^15.6.0",
"react-addons-transition-group": "^15.6.0",
"react-burger-menu": "^2.1.4",
"react-dom": "^15.6.1",
"react-icons": "^2.2.5",
"react-loading": "^0.1.4",
"react-modal": "^2.2.4",
"react-router-dom": "^4.1.2",
"react-scripts": "1.0.11",
"reactstrap": "^4.8.0",
"redux-thunk": "^2.2.0"
},
"scripts": {
"build-css": "node-sass-chokidar src/ -o src/",
"watch-css": "npm run build-css && node-sass-chokidar src/ -o src/ --watch --recursive",
"start-js": "react-scripts start",
"start": "npm-run-all -p watch-css start-js",
"build": "npm run build-css && react-scripts build",
"test": "react-scripts test --env=jsdom",
"eject": "react-scripts eject"
},
"devDependencies": {
"npm-run-all": "^4.0.2"
}
}
I encountered this when I moved from react-bootstrap (using bootstrap 3.3.7) to reactstrap.
Even though my package.json indicated "bootstrap": "^4.0.0-alpha.6" my configuration was still pulling 3.3.7.
I deleted my node_modules/bootstrap folder, did an npm install and it started working.
Maybe something to check?