[react and Material-UI]: Appbar(NavBar) not picking material ui css - css

I just want to create a simple Nav bar using Material-UI. but unfortunately, it's not working. The method mentioned in the docs, consist of a lot of extra code which I do not need. Wondering if it's possible to achieve the same without all that code.
these are the list of dependencies installed in 'package.json'
"dependencies": {
"#material-ui/core": "^3.9.2",
"react": "^16.8.3",
"react-dom": "^16.8.3",
"react-redux": "^6.0.1",
"react-router-dom": "^4.3.1",
"react-scripts": "2.1.5",
"redux": "^4.0.1"
},
this is my navbar component
import React from 'react'
import { Link } from 'react-router-dom';
const Navbar = () => {
return(
<div>
<nav className="nav-wrapper">
<div className="container">
<Link to="/" className="brand-logo">Shopping</Link>
<ul className="right">
<li><Link to="/">Shop</Link></li>
<li><Link to="/cart">My cart</Link></li>
<li><Link to="/cart"><i className="material-icons">shopping_cart</i></Link></li>
</ul>
</div>
</nav>
</div>
)
}
export default Navbar;
and this is my 'App.js'
import React, { Component } from 'react';
import Navbar from './components/Navbar';
import {BrowserRouter, Route, Switch} from 'react-router-dom'
import Home from './components/Home'
import Cart from './components/Cart'
class App extends Component {
render() {
return (
<BrowserRouter>
<div className="App">
<Navbar/>
<Switch>
<Route exact path="/" component={Home}/>
<Route path="/cart" component={Cart}/>
</Switch>
</div>
</BrowserRouter>
);
}
}
export default App;

Related

React Boostrap 4 css module not getting applied to Navbar

I am having trouble trying to change my navbar's color.. Am I missing any steps?
this is the component i am trying to render:
import React from 'react';
import { Nav, Navbar } from 'react-bootstrap';
import styles from './MainMenu.module.css';
const Topbar = () => {
return(
<Navbar className={styles.mainBar}>
<Navbar.Brand>Restaurant</Navbar.Brand>
<Nav.Link>Menu</Nav.Link>
</Navbar>
);
}
export default Topbar;
this is the CSS module
.mainBar{
background-color: rgb(255, 153, 0);
}
this is the dependencies in the package.json i have for the project:
"dependencies": {
"#testing-library/jest-dom": "^5.12.0",
"#testing-library/react": "^11.2.7",
"#testing-library/user-event": "^12.8.3",
"bootstrap": "^4.6.0",
"react": "^17.0.2",
"react-bootstrap": "^1.6.1",
"react-dom": "^17.0.2",
"react-scripts": "4.0.3",
"web-vitals": "^1.1.2"
}
bootstrap is getting applied as i imported to the index.js...
import React from 'react';
import ReactDOM from 'react-dom';
import './index.css';
import '../node_modules/bootstrap/dist/css/bootstrap.min.css';
import App from './App';
import reportWebVitals from './reportWebVitals';
ReactDOM.render(
<React.StrictMode>
<App />
</React.StrictMode>,
document.getElementById('root')
);
reportWebVitals();
You are importing a css file into styles but unlike js modules which export functions, objects, etc your css file doesn't export such things. you need to remove styles from your import and just import your css file like this
import './MainMenu.module.css';
And in your className simply mention your class name
<Navbar className="mainBar">
It works fine

reactstrap navbar icon doesn't appear

I'm trying to make a navbar to my react app and I was using this code:
ReactStrap navbar component
However, the navbar icon doesn't appear in small devices view.
small devices navbar (icon is not show)
My NavBar.js
import React, {Component} from 'react';
import NavItems from "./NavItems";
import { Collapse, Navbar, NavbarToggler, NavbarBrand, Nav, NavItem, NavLink } from 'reactstrap';
class NavBar extends Component {
constructor(props) {
super(props);
this.toggleNavbar = this.toggleNavbar.bind(this);
this.state = {
collapsed: true
};
}
toggleNavbar() {
this.setState({
collapsed: !this.state.collapsed
});
}
render() {
return (
<div>
<Navbar color="faded" light>
<NavbarBrand href="/" className="mr-auto">Hoje para jantar</NavbarBrand>
<NavbarToggler onClick={this.toggleNavbar} className="mr-2" />
<Collapse isOpen={!this.state.collapsed} navbar>
<Nav navbar>
<NavItems>
<NavLink href="/components/">Components</NavLink>
</NavItems>
</Nav>
</Collapse>
</Navbar>
</div>
);
}
}
export default NavBar;
I guess you are missing the expand prop in Navbar component.
If you set expand prop to "lg" for example, navbar will add the icon for devices with smaller width than "lg", which is smaller than 1200px:
<Navbar color="light" light expand="lg">...</Navbar
You can also set the expand prop to "md" or "sm".
I tried recreating your problem using create-react-app and put together the following package.json
{
"name": "so2",
"version": "0.1.0",
"private": true,
"dependencies": {
"bootstrap": "^4.0.0-beta.2",
"jquery": "^3.2.1",
"react": "^16.2.0",
"react-dom": "^16.2.0",
"react-popper": "^0.7.4",
"react-scripts": "1.0.17",
"reactstrap": "^5.0.0-alpha.4"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test --env=jsdom",
"eject": "react-scripts eject"
}
}
My version of NavBar.js, whcih is a copy of yours without lines 2, and 25-27:
import React, {Component} from 'react';
// import NavItems from "./NavItems";
import { Collapse, Navbar, NavbarToggler, NavbarBrand, Nav, NavItem, NavLink } from 'reactstrap';
class NavBar extends Component {
constructor(props) {
super(props);
this.toggleNavbar = this.toggleNavbar.bind(this);
this.state = {
collapsed: true
};
}
toggleNavbar() {
this.setState({
collapsed: !this.state.collapsed
});
}
render() {
return (
<div>
<Navbar color="faded" light>
<NavbarBrand href="/" className="mr-auto">Hoje para jantar</NavbarBrand>
<NavbarToggler onClick={this.toggleNavbar} className="mr-2" />
<Collapse isOpen={!this.state.collapsed} navbar>
<Nav navbar>
{/*<NavItems>*/}
{/*<NavLink href="/components/">Components</NavLink>*/}
{/*</NavItems>*/}
</Nav>
</Collapse>
</Navbar>
</div>
);
}
}
export default NavBar;
And got this
In my own experience I have noticed that it is quite easy to cause a css conflict by experimenting or implementing your desired changes. What I would do is test any css changes you have made on a fresh project, spot the trouble, then fix it in the original (working) project.
I am hoping this helps you since I just spent the entire morning trying to experiment with multiple css files and react components. I wish I could be more helpful but I'm not allowed to post comments yet. Good luck!

React Router v4 + Meteor createContainer - routes not rendering

I'm experiencing an issue using Meteor's createContainer with React Router v4. I've used it successfully with v3, but when I try to set up routing with v4 it loads the Main route and then won't render anything else. If I change App to a functional stateless component and bypass the data layer, the routing works fine, so I know it's something in there.
App.jsx:
import React from 'react'
import {Navigation} from './Navigation'
import {Grid, MenuItem} from 'react-bootstrap'
import {LinkContainer} from 'react-router-bootstrap'
import { createContainer } from 'meteor/react-meteor-data'
import {Products} from '../api/products'
import {Main} from './Main'
class App extends React.Component {
render () {
const {ready, products} = this.props;
if (!ready) return <h1>Loading...</h1>
const vendorsList = [...new Set(products.map(item => item.vendor).filter(i => !!i))]
const vendors = vendorsList.map((item, index) => <LinkContainer key={index} to={`/vendors/${item}`}><MenuItem eventKey={(index+1) / 10 + 4}>{item}</MenuItem></LinkContainer>)
return (
<div>
<Navigation vendors={vendors} />
<Grid>
<Main products={products} />
</Grid>
</div>
)
}
}
export default createContainer(({params}) => {
const handle = Meteor.subscribe('products');
return {
ready: handle.ready(),
products: Products.find({}, {sort: {name: 1}}).fetch()
};
}, App);
Navigation.jsx:
import React from 'react'
import {NavLink} from 'react-router-dom'
import {Navbar, Nav, NavItem, NavDropdown, MenuItem} from 'react-bootstrap'
import {LinkContainer} from 'react-router-bootstrap'
export const Navigation = ({vendors}) => (
<Navbar>
<Navbar.Header>
<LinkContainer to='/'><Navbar.Brand>IM 0.1</Navbar.Brand></LinkContainer>
</Navbar.Header>
<Nav>
<NavDropdown eventKey={1} title='Products' id='basic-nav-dropdown'>
<LinkContainer to='/products'><MenuItem eventKey={1.1}>Products List</MenuItem></LinkContainer>
<LinkContainer to='/products/new'><MenuItem eventKey={1.2}>Enter New Product</MenuItem></LinkContainer>
</NavDropdown>
<NavItem eventKey={2}>Inventory</NavItem>
<NavDropdown eventKey={3} title='Invoices' id='basic-nav-dropdown'>
<MenuItem eventKey={3.1}>Enter New Invoice</MenuItem>
<MenuItem divider />
<MenuItem eventKey={3.2}>Manage Invoices...</MenuItem>
</NavDropdown>
<NavDropdown eventKey={4} title='Vendors' id='basic-nav-dropdown'>
{vendors}
<MenuItem divider />
<MenuItem eventKey={(vendors.length + 1)/10 + 4}>Vendors List...</MenuItem>
</NavDropdown>
</Nav>
</Navbar>
)
main.js:
import React from 'react'
import { Meteor } from 'meteor/meteor'
import { render } from 'react-dom'
import {BrowserRouter} from 'react-router-dom'
import App from '../imports/ui/App'
Meteor.startup(() => {
render((
<BrowserRouter>
<App />
</BrowserRouter>
), document.getElementById('render-target'));
});
I know I'm missing something that's probably super basic, and it's driving me nuts. Thanks.
Try wrapping App.jsx with withRouter
Also note that Meteor createContainer function has been replaced by withTracker
import React from 'react'
import { withTracker } from 'meteor/react-meteor-data';
import { withRouter } from 'react-router-dom';
...
class App extends React.Component {
...
}
export default withRouter(withTracker(({params}) => {
...
})(App));

ReactCSSTransitionGroup leave animation not applying to Route

Entering animations are applied correctly. Component seems to unmount prior to applying any leave, leave-active classes.
componentWillMount() {
this.setState({
routes: [(<Route exact path='/' component={HomeView}/>),
(<Route exact path='/account' component={YourAccountView}/>),
(<Route exact path='/settings' component={SettingsView}/>),
(<Route exact path='/about' component={AboutView}/>),
(<Route exact path='/machine/:_id' component={MachineDetailView}/>),
(<Route exact path='/floorview' component={FloorView}/>)]
})
}
render() {
return (
<div>
<NavBar/>
<div style={{position: 'relative', flexGrow: 1 , marginTop:40+'px'}}>
<ReactCSSTransitionGroup
transitionName="pageSlider"
transitionEnter={true}
transitionLeave={true}
transitionEnterTimeout={500}
transitionLeaveTimeout={500}>
{this.state.routes
.filter((e)=> e.props.path===this.context.router.history.location.pathname )
.map((e)=> React.cloneElement(e, { key: this.context.router.history.location.pathname} ))}
</ReactCSSTransitionGroup>
</div>
</div>
);
}
I can't tell if this is a ReactCSSTransitionGroup thing, or a React-Router v4 mounting/unmounting thing. Has anyone run into and solved similar issue?
this case works:
index.js
import React from 'react';
import ReactDOM from 'react-dom';
import {BrowserRouter, Route} from 'react-router-dom';
import App from './App';
import './index.css';
ReactDOM.render(
<BrowserRouter>
<Route path="/" component={App}/>
</BrowserRouter>
,
document.getElementById('root')
);
App.js
import React, { Component } from 'react';
import { Switch, Route, Link } from 'react-router-dom';
import ReactCSSTransitionGroup from 'react-addons-css-transition-group';
import './App.css';
const One = ({match}) => (
<h2>{match.url}</h2>
)
const Two = ({match}) => (
<h2>{match.url}</h2>
)
const Three = ({match}) => (
<h2>{match.url}</h2>
)
const Four = ({match}) => (
<h2>{match.url}</h2>
)
const MyNav = () => (
<div>
<Link to='/One'>One</Link>
<Link to='/Two'>Two</Link>
<Link to='/Three'>Three</Link>
<Link to='/Four'>Four</Link>
</div>
)
class App extends Component {
componentWillMount() {
this.setState({routeKey:this.props.location.pathname})
this.setState({routes: [
(<Route exact path="/One" component={One}/>),
(<Route exact path="/Two" component={Two}/>),
(<Route exact path="/Three" component={Three}/>),
(<Route exact path="/Four" component={Four}/>)
]})
}
render() {
return (
<div className="App">
<div>
<MyNav/>
<ReactCSSTransitionGroup
transitionName="PageSlider"
transitionEnterTimeout={0}
transitionLeaveTimeout={150}>
<Switch key={this.props.location.pathname}>
<Route exact path="/One" component={One}/>
<Route exact path="/Two" component={Two}/>
<Route exact path="/Three" component={Three}/>
<Route exact path="/Four" component={Four}/>
</Switch>
</ReactCSSTransitionGroup>
</div>
</div>
);
}
componentWillReceiveProps(newProps) {
this.setState({routeKey:newProps.location.pathname})
}
}
export default App;
specify .PageSlider-enter, .PageSlider-enter.PageSlider-enter-active, .PageSlider-leave, .PageSlider-leave.PageSlider-leave-active accordingly

React Router not displaying my components

I have read all the other questions with the same issue, but it just doesn't work for me.
index.html
<body>
<h1>Index</h1>
<div id="app"></div>
</body>
client/app.jsx
import React from 'react';
import { render } from 'react-dom';
import { Router, Route, IndexRoute, browserHistory } from 'react-router';
import { Accounts, STATES } from 'meteor/std:accounts-ui';
import { MainLayout } from '../imports/ui/layouts/main.jsx';
import { IndexPage } from '../imports/ui/components/index.jsx';
import { NotFoundPage } from '../imports/ui/components/errors/not-found.jsx';
Meteor.startup( () => {
render(
<Router history={ browserHistory }>
<Route path="/" component={ MainLayout }>
<IndexRoute component={ IndexPage } />
<Route path="signin" component={ Accounts.ui.LoginForm } formState={ STATES.SIGN_IN } />
<Route path="signup" component={ Accounts.ui.LoginForm } formState={ STATES.SIGN_UP } />
</Route>
<Route path="*" component={ NotFoundPage } />
</Router>,
document.getElementById('app')
);
});
imports/ui/layouts/main.jsx
import { Component } from 'react';
export default class MainLayout extends Component {
render() {
return (
<div>
<h2>Main Layout</h2>
{this.props.children}
</div>
);
}
}
imports/ui/components/index.jsx
import { Component } from 'react';
export default class IndexPage extends Component {
render() {
return (
<div>Index Page</div>
);
}
}
imports/ui/components/errors/not-found.jsx
import { Component } from 'react';
export default class NotFoundPage extends Component {
render() {
return (
<div>404 - Not found!</div>
);
}
}
So, going to any URL except /signin or /signup does not show anything but what's in index.html (i.e. react does not render anything)
Moreoever, /signin does not render MainLayout at all.
I have tried looking around, re-read the docs, etc. I don't see anything wrong with my code, and there is no error whatsoever. So, why isn't it working?
(Note: I have Meteor 1.3.2.4 with all latest npm modules and packages installed yesterday.)
Well, I went walking outside (it's finally sunny and warm!) and came back. The only difference I saw between my code and the example here was the export statement... or export default to be more precise.
Changing from
import { MainLayout } from '../imports/ui/layouts/main.jsx';
to
import MainLayout from '../imports/ui/layouts/main.jsx';
was my mistake.

Resources