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!
Related
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
I am using Next.js with Material-UI as the framework.
I have Layout component that wraps the contents with Material-UI <Container>.
I would like to override the style of Layout that limits the width of background, so that the background would extend to the full screen.
components/Layout.js
import { Container } from '#material-ui/core';
export default function Layout({ children }) {
return <Container>{children}</Container>;
}
pages/_app.js
import Layout from '../components/Layout';
...
<Layout>
<Component {...pageProps} />
</Layout>
...
pages/index.js
export default function App() {
return (
<div style={{ backgroundColor: "yellow" }}>
Home Page
</div>
)
}
Using Layout component comes in handy in most cases but sometimes I do want to override the certain styles of Layout from the child component.
In this case, how do I override the style of Layout component that puts the limit on maxWidth?
I tried to add {width: '100vw'} inside the style of pages/index.js, but did not work.
Any help would be appreciated.
Link to the SandBox
Using React Context is how I've solved this issue.
context/ContainerContext.js
import React from 'react';
const ContainerContext = React.createContext();
export default ContainerContext;
components/Layout.js
import React, { useState } from 'react';
import { Container } from '#material-ui/core';
import ContainerContext from '../context/ContainerContext';
export default function Layout({ children }) {
const [hasContainer, setHasContainer] = useState(true);
const Wrapper = hasContainer ? Container : React.Fragment;
return (
<ContainerContext.Provider value={{ hasContainer, setHasContainer }}>
<Wrapper>{children}</Wrapper>
</ContainerContext.Provider>
);
}
pages/index.js
import { useContext, useLayoutEffect } from 'react';
import ContainerContext from '../context/ContainerContext';
export default function App() {
const { setHasContainer } = useContext(ContainerContext);
// Where the magic happens!
useLayoutEffect(() => {
setHasContainer(false);
}, []);
return (
<div style={{ backgroundColor: "yellow" }}>
Home Page
</div>
);
}
i'm trying to use the antd tooltip with vue3
<a-tooltip :mouseEnterDelay="1">
<template #title> prompt text </template>
<a-button type="danger" shape="circle" #click="handleDelete(record.id)">
<template #icon><DeleteOutlined /></template>
</a-button>
</a-tooltip>
the code looks ok, but this is the result:
kinda ugly, i'm also using the same version of the docs:
"dependencies": {
"ant-design-vue": "^2.0.0-rc.6",
"axios": "^0.21.1",
"lockr": "^0.8.5",
"lodash": "^4.17.20",
"vue": "^3.0.0",
"vue-i18n": "^9.0.0-beta.17",
"vue-router": "^4.0.0-0",
"vuex": "^4.0.0-0"
},
edit: added main.ts full code.
import { createApp } from 'vue'
import App from '#/App.vue'
import router from '#/router'
import store from '#/store'
import 'ant-design-vue/dist/antd.css'
import '#/style/index.scss'
import { createI18n } from 'vue-i18n'
import messages from '#/assets/i18n.json'
import { Layout, Menu, Select, Avatar, Dropdown, Tag, Divider, Table, Tooltip, Button, message, Popconfirm } from 'ant-design-vue'
const i18n = createI18n({
legacy: false,
locale: 'en',
messages
})
const app = createApp(App)
app.config.globalProperties.$message = message
app
.use(i18n)
.use(store)
.use(router)
.use(Layout)
//...all the other use of ant
.mount('#app')
anyone can help?
I'm writing some simple reusable component for our React(with MaterialUI) application.
The problem is, that i want to allow different styles of this same reusable component, to be customized via props, by the consuming component.
This is some of the code:
import { withStyles } from '#material-ui/core';
const styles = theme => ({
image: {
maxHeight: '200px'
}
});
render() {
const classes = this.props.classes
return (
<div>
...
<img className={classes.image} src={this.state.filePreviewSrc} alt="" />
...
</div>
);
}
Let's say, i want to allow the programmer to customize the appearance of classes.image. Can the hard-coded image class be overwritten somehow?
Is using withStyles api is even the correct approach, for creating components whose appearance can be customized by the consuming component/programmer?
There are three main approaches available for how to support customization of styles:
Leverage props within your styles
Leverage props to determine whether or not certain classes should be applied
Do customization via withStyles
For option 3, the styles of the wrapping component will be merged with the original, but the CSS classes of the wrapping component will occur later in the <head> and will win over the original.
Below is an example showing all three approaches:
ReusableComponent.js
import React from "react";
import { withStyles } from "#material-ui/core/styles";
const styles = {
root: props => ({
backgroundColor: props.rootBackgroundColor
? props.rootBackgroundColor
: "green"
}),
inner: props => ({
backgroundColor: props.innerBackgroundColor
? props.innerBackgroundColor
: "red"
})
};
const ReusableComponent = ({ classes, children, suppressInnerDiv = false }) => {
return (
<div className={classes.root}>
Outer div
{suppressInnerDiv && <div>{children}</div>}
{!suppressInnerDiv && (
<div className={classes.inner}>
Inner div
<div>{children}</div>
</div>
)}
</div>
);
};
export default withStyles(styles)(ReusableComponent);
index.js
import React from "react";
import ReactDOM from "react-dom";
import { withStyles } from "#material-ui/core/styles";
import ReusableComponent from "./ReusableComponent";
const styles1 = theme => ({
root: {
backgroundColor: "lightblue",
margin: theme.spacing(2)
},
inner: {
minHeight: 100,
backgroundColor: "yellow"
}
});
const Customization1 = withStyles(styles1)(ReusableComponent);
const styles2 = {
inner: {
backgroundColor: "purple",
color: "white"
}
};
const Customization2 = withStyles(styles2)(ReusableComponent);
function App() {
return (
<div className="App">
<ReusableComponent>Not customized</ReusableComponent>
<Customization1>Customization 1 via withStyles</Customization1>
<Customization2>Customization 2 via withStyles</Customization2>
<ReusableComponent rootBackgroundColor="lightgrey" suppressInnerDiv>
Customization via props
</ReusableComponent>
</div>
);
}
const rootElement = document.getElementById("root");
ReactDOM.render(<App />, rootElement);
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;