Reactstrap NavDropdown component doesn't open - reactjs

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?

Related

JSX not showing in browser

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

React create stack navigator

I am using react native and expo. I am getting error message after run expo and it says:
Error: Creating a navigator doesn't take an argument. Maybe you are trying to use React Navigation 4 API with React Navigation 5? See https://reactnavigation.org/docs/upgrading-from-4.x for migration guide.
App.js
import React from 'react';
import { createStackNavigator } from '#react-navigation/stack';
import Home from './components/Home.js'
import Movie from './components/Movie.js'
export default class App extends React.Component {
render() {
return <AppNavigator />
}
}
const AppNavigator = createStackNavigator({
'Home': Home,
'Movie': Movie,
})
Package.json
{
"main": "node_modules/expo/AppEntry.js",
"scripts": {
"start": "expo start",
"android": "expo start --android",
"ios": "expo start --ios",
"web": "expo start --web",
"eject": "expo eject"
},
"dependencies": {
"#react-native-community/masked-view": "^0.1.6",
"#react-navigation/native": "^5.4.2",
"#react-navigation/stack": "^5.3.9",
"expo": "~37.0.3",
"react": "~16.9.0",
"react-dom": "~16.9.0",
"react-native": "https://github.com/expo/react-native/archive/sdk-37.0.1.tar.gz",
"react-native-gesture-handler": "^1.6.1",
"react-native-reanimated": "^1.7.1",
"react-native-safe-area-context": "^0.7.3",
"react-native-screens": "^2.2.0",
"react-native-web": "~0.11.7"
},
"devDependencies": {
"babel-preset-expo": "~8.1.0",
"#babel/core": "^7.8.6"
},
"private": true
}
Could you please help to resolve this issue?
I am waiting for your response.
Thanks in advance
You are creating the navigator in the old way, the code for V5 which you are using should be like below.
You will have to import NavigationContainer as well.
import { NavigationContainer } from '#react-navigation/native';
const Stack = createStackNavigator();
function App() {
return (
<NavigationContainer>
<Stack.Navigator>
<Stack.Screen name="Home" component={Home} />
<Stack.Screen name="Movie" component={Movie} />
</Stack.Navigator>
</NavigationContainer>
);
}
More reference can be found here
https://reactnavigation.org/docs/stack-navigator/

Element type is invalid when i wrap my app with <Provider>

I get an error when i try wrapping my <App> with <Provder>, here is the code
import React from 'react';
import ReactDOM from 'react-dom';
import { createStore } from 'redux';
import { Provider } from 'react-redux';
import './index.css';
import App from './App';
import registerServiceWorker from './registerServiceWorker';
import reducer from './store/reducer';
const store = createStore(reducer);
ReactDOM.render(<Provider store={store}><App /></Provider>, document.getElementById('root'));
registerServiceWorker();
Here is the error i get after i use <Provider>:
And here is my package.json :
{
"name": "react-complete-guide",
"version": "0.1.0",
"private": true,
"dependencies": {
"react": "^16.7.0",
"react-dom": "^16.0.0",
"react-redux": "^6.0.0",
"react-scripts": "1.0.13",
"redux": "^4.0.1"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test --env=jsdom",
"eject": "react-scripts eject"
}
}
In the end i realize this is probably related to a conflict of version in my package.json, but now matter how much i update each package i can't fix this, i also tried common fixes suggested in other threads with a similar problem but still no luck.
Thanks.
Update 01 :
App.js :
import React, { Component } from 'react';
import Counter from './containers/Counter/Counter';
import './App.css';
class App extends Component {
render() {
return (
<div className="App">
<Counter />
</div>
);
}
}
export default App;

Reactstrap Card component throws error

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 :)

React.CreateElement issue on react-router-v4, not v4-alpha4

I'm trying to figure out why a specific function is working with the alpha version of React Router but not the current v4 version
index.js
import React from 'react';
import { render } from 'react-dom';
import { BrowserRouter, Match } from 'react-router';
import NotFound from './components/NotFound';
const Root = () => {
return (
<BrowserRouter>
<div>
<Match exactly pattern="/" component={NotFound} />
</div>
</BrowserRouter>
)
}
render(<Root />, document.querySelector('#root'));
package.json
{
"name": "rbm-dashboard",
"version": "0.1.0",
"private": true,
"dependencies": {
"material-ui": "^0.17.0",
"react": "^15.4.2",
"react-tap-event-plugin": "^2.0.1",
"react-dom": "^15.4.2"
},
"devDependencies": {
"react-router": "4.0.0-alpha.4",
"react-scripts": "0.9.3"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test --env=jsdom",
"eject": "react-scripts eject"
}
}
NotFound.js
import React from 'react';
class NotFound extends React.Component {
render() {
return (
<h2>Not Found</h2>
)
}
}
export default NotFound;
When i switch the react-router version from 4.0.0-alpha.4 to ^4.0.0, i get the following errors
I don't have an issue using the alpha version, however I would like to figure out what the issue is so i can learn from it. Also if needed i can push the code to github if anyone wants to look at it locally.
Thank you
Because Match isn't a thing anymore. Change Match pattern to Route path and you should be good. Also, import everything from react-router-dom not react-router.

Resources