I am building a new ReactJS app and tried to use react-bootstrap for the UI. I essentially installed yarn add react-bootstrap bootstrap yarn add jquery popper.js" to have everything installed. My index.js` looks as follows:
// Importing the Bootstrap CSS
import 'bootstrap/dist/css/bootstrap.css';
import $ from 'jquery';
import Popper from 'popper.js';
import React from 'react';
import ReactDOM from 'react-dom';
import App from './app/App';
import * as serviceWorker from './serviceWorker';
import './index.css';
ReactDOM.render(
<React.StrictMode>
<App />
</React.StrictMode>,
document.getElementById('root')
);
serviceWorker.unregister();
App.js is quite straightforward:
import React from 'react';
import AppHeader from "./layout/AppHeader";
import RouterOutlet from "../router/Router";
import AppFooter from "./layout/AppFooter";
function App() {
return (
<div>
<div className="wrap">
<AppHeader />
<div className="container">
<RouterOutlet />
</div>
</div>
<AppFooter />
</div>
);
}
export default App;
The interesting part now is the header not working as expected, its containing the NavBar
import React from 'react';
import NavDropdown from 'react-bootstrap/NavDropdown'
import Navbar from "react-bootstrap/Navbar";
class AppHeader extends React.Component {
render () {
return (
<Navbar variant="dark" sticky="top">
<Navbar.Brand>
<img
src="/logo192.png"
width="30"
height="30"
className="d-inline-block align-top"
alt=" "/>
</Navbar.Brand>
<NavDropdown bg="black" title="Menu" id="collasible-nav-dropdown" style={{color: "#5299d3"}}>
<NavDropdown.Item href="/">Home</NavDropdown.Item>
<NavDropdown.Item href="/time">Time</NavDropdown.Item>
<NavDropdown.Item href="/login">Login</NavDropdown.Item>
<NavDropdown.Item href="/logout">Logout</NavDropdown.Item>
</NavDropdown>
</Navbar>
);
}
}
export default AppHeader
This is the Footer
import * as React from 'react';
class AppFooter extends React.Component {
render() {
return (
<footer className="footer">
<div className="container">
<p className="pull-left">© My Company</p>
<p className="pull-right">asd</p>
</div>
</footer>
);
}
}
export default AppFooter;
However the bootstrap styles are not applied, the bar is white without any background at all. Also other things such as "pull-right" classes in the footer don't work. I can see in the browser though that the CSS is loaded as a inline CSS file.
Question: why are the styles not applied to my components so far?
To add background to Navbar, add 'bg="primary" ' to your code in AppHeader like this-
<Navbar bg="primary" variant="dark" sticky="top">
This will make you background blue.
Related
Hi guys I am new to React .Just want to render my component into app component but unable to do so here is my code :
conditionalView.js:
import React, { Component } from 'react'
export class conditionalView extends Component {
render() {
return (
<div>
<h2>Conditional View</h2>
</div>
)
}
}
export default conditionalView
App.js
import logo from './logo.svg';
import './App.css';
import Bike from './bike';
import Car from './car';
import FnEvent from './FnEvent';
import ClsEvent from './ClsEvent';
import ClsEventBind from './ClsEventBind';
import conditionalView from './conditionalView';
function App() {
return (
<div className="App">
<header className="App-header">
{/* <img src={logo} className="App-logo" alt="logo" />
<Bike name="Caliber 125" />
<Car name="Maruti 800" /> */}
{/* <FnEvent />
<ClsEvent /> */}
{/* <ClsEventBind /> */}
<conditionalView />
</header>
</div>
);
}
export default App;
index.js
import React from 'react';
import ReactDOM from 'react-dom';
import './index.css';
import App from './App';
import reportWebVitals from './reportWebVitals';
import reactDom from 'react-dom';
ReactDOM.render(
<React.StrictMode>
<App />
</React.StrictMode>,
document.getElementById('root')
);
reportWebVitals();
Basically i want to render content of conditionView.js component into App.js but it is not picking up the content.
In react custom components are supposted to start with capital letters. For standard HTML elements, like div, span use lower case.
Also, no need to export twice from ConditionalView.
You can export at the same time of declaration along with the default keyword.
import React, { Component } from 'react'
export default class conditionalView extends Component {
render() {
return (
<div>
<h2>Conditional View</h2>
</div>
)
}
}
Your component should be PascalCase: ConditionalView, not conditionalView.
This might be happening because you are exporting component more than once
export class conditionalView extends Component {
remove the export keyword from here
React components should always start with a capital letter, so rename <conditionalView /> to <ConditionalView />,
and update the import statement too;
import ConditionalView from './conditionalView';
First of all, I am truly new in reactjs. I am trying to design my react project using bootstrap. For some reasons it's not working as it should be. Below is my code:
App.js
import React from 'react';
import './App.css';
import Menu from '../components/Menu/Menu';
function App() {
return (
<div className="App">
<Menu />
</div>
);
}
export default App;
Menu.js
import React from 'react'
import * as ReactBootstrap from 'react-bootstrap'
const Menu = () => {
return (
<div id="menu_container">
<ReactBootstrap.Navbar bg="dark" variant="dark">
<ReactBootstrap.Navbar.Brand href="#home">Navbar</ReactBootstrap.Navbar.Brand>
<ReactBootstrap.Nav className="mr-auto">
<ReactBootstrap.Nav.Link href="#home">Home</ReactBootstrap.Nav.Link>
<ReactBootstrap.Nav.Link href="#features">Features</ReactBootstrap.Nav.Link>
<ReactBootstrap.Nav.Link href="#pricing">Pricing</ReactBootstrap.Nav.Link>
</ReactBootstrap.Nav>
<ReactBootstrap.Form inline>
<ReactBootstrap.FormControl type="text" placeholder="Search" className="mr-sm-2" />
<ReactBootstrap.Button variant="outline-light">Search</ReactBootstrap.Button>
</ReactBootstrap.Form>
</ReactBootstrap.Navbar>
</div>
)
}
export default Menu;
Index.js
import React from 'react';
import ReactDOM from 'react-dom';
import './index.css';
import App from './containers/App';
import * as serviceWorker from './serviceWorker';
import 'bootstrap/dist/css/bootstrap.min.css';
ReactDOM.render(
<React.StrictMode>
<App />
</React.StrictMode>,
document.getElementById('root')
);
serviceWorker.unregister();
Current Output:
Current output
Output I want:
Output I want
Make sure you add the CDN link in index.html.
Although you found it this would help others.
<link
rel="stylesheet"
href="https://maxcdn.bootstrapcdn.com/bootstrap/4.2.1/css/bootstrap.min.css"
integrity="sha384-GJzZqFGwb1QTTN6wy59ffF1BuGJpLSa9DkKMp0DgiMDm4iYMj70gZWKYbI706tWS"
crossorigin="anonymous"/>
or add this in app.css
#import "bootstrap/dist/css/bootstrap.css";
You have some .css files that are imported in App.js and index.js. Maybe they are overriding some bootstrap rules.
I took your code here https://codesandbox.io/s/test-react-bootstrap-75rsd, removed the App.css and index.css imports and it looks just as you want.
Through the research that I have done on React Router, it seems every example that's online the React Router code is always stored in the App.js file.
What if you're wanting to use routers for links in the sidebar, header or footer?
Specifically, this is the example that I am learning from to implement React Router:
https://www.freecodecamp.org/news/a-complete-beginners-guide-to-react-router-include-router-hooks/
Desired result: Link a route to the Sitemap page with the component attribute.
App.js
import React from 'react';
import Header from './components/Header';
import Hero from './components/Hero';
import Main from './components/Main';
import Footer from './components/Footer';
import Breadcrumbs from './components/Breadcrumbs';
import Recent from './components/Recent';
function App() {
return (
<div className="App">
<Header/>
<Breadcrumbs/>
<Hero/>
<Main/>
<Recent/>
<Footer/>
</div>
);
}
export default App;
Footer.js
import React from 'react';
import { BrowserRouter, Route, Link } from 'react-router-dom';
import Container from 'react-bootstrap/Container';
import Row from 'react-bootstrap/Row';
import Col from 'react-bootstrap/Col';
import SitemapPage from '../components/pages/SitemapPage';
const Footer = () => {
return(
<footer>
<Container id="footer">
<Row>
<Col className="col menu" xs="6" md="12">
<p className="text-right">
<BrowserRouter>
<Link to="/sitemap/">Sitemap</Link>
Terms
Privacy
<Route path="/sitemap/" component={SitemapPage} />
</BrowserRouter>
</p>
</Col>
</Row>
</Container>
</footer>
);
};
export default Footer;
SitemapPage.js
import React from 'react';
import Header from '../Header';
import Breadcrumbs from '../Breadcrumbs';
import Footer from '../Footer';
const SitemapPage = () => {
return(
<div className="sitemap page">
<Header />
<Breadcrumbs />
<p>Sitemap page</p>
<Footer />
</div>
);
};
export default SitemapPage;
It's imaginable that I am not the only developer that wants to set up routes with links throughout the website.
Without giving me the complete answer, could someone leave a comment with a good example that's similar to this?
I'm not sure what is your expectation, though here is some input:
What is in side your Router element will re-render when the route changes. So if you have your Router element in your Header element, when the route changes (user clicks the link), only your Header element will be re-rendered.
It's OK to have your Router element at the App element, and still you can put your link inside your header element.
I have just started to learn React.js and material-ui. I was using this to display an icon on screen. However the icon is not displayed on the screen and the screen appears to be blank.
Here is my code:
index.js
import React from 'react';
import ReactDOM from 'react-dom';
import MuiThemeProvider from 'material-ui/styles/MuiThemeProvider';
import {IconButtonExampleSimple} from './IconButtonExampleSimple.js';
class App extends React.Component{
render()
{
return(
<MuiThemeProvider>
<IconButtonExampleSimple />
</MuiThemeProvider>
);
}
}
ReactDOM.render(
<App/>,
document.getElementById('root')
);
IconButtonExampleSimple.js
import React from 'react';
import IconButton from 'material-ui/IconButton';
export class IconButtonExampleSimple extends React.Component{
render ()
{
return (<div>
<IconButton iconClassName="muidocs-icon-custom-github" />
<IconButton iconClassName="muidocs-icon-custom-github"
disabled={true} />
</div>
);
}
}
Add this line to your index.html
<link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">
You are including the Material-UI part of the icon buttons but those are refering to google's Material-Icons so you must link it in your index file.
I am developing a React Application and I added React Redux Localization library from AlternativaPlatform for localization and text translation to my application. The library works fine for simple text because I will need to call <Translate value="hello" /> every time I want to translate the text and this component will return the string within the <span></span> tag. I am having trouble to translate all texts which are defined in the class component. I have done a research and read the documentation but I did not find a solution since yesterday.
What I have been trying to do, I created the following files in my project;
index.js
import React from 'react';
import ReactDOM from 'react-dom';
import { createStore, combineReducers } from 'redux';
import { Provider } from 'react-redux';
import { localeReducer } from 'react-redux-localization';
import App from './App';
import './index.css';
const rootReducer = combineReducers({ locale: localeReducer });
const store = createStore(rootReducer);
ReactDOM.render(
<Provider store={store}>
<App />
</Provider>,
document.getElementById('root')
);
App.js
import React, { Component } from 'react';
import './App.css';
import { BrowserRouter, Route } from 'react-router-dom';
import Home from './components/Home';
class App extends Component {
render() {
return (
<BrowserRouter>
<div>
<Route exact path="/" component={Home} />
</div>
</BrowserRouter>
);
}
}
export default App;
Home.js
import React, { Component } from 'react';
import NavigationBar from './Navbar';
import Footer from './Footer';
/*
* Home component
*/
export default class Home extends Component {
render() {
return (
<div>
<NavigationBar />
<Footer />
</div>
);
}
}
Navbar.js
import React, { Component } from 'react';
import ReactDOM from 'react-dom';
import { NavLink } from 'react-router-dom';
import { Nav, Navbar, NavItem, OverlayTrigger, Popover, InputGroup, Button, FormControl, Tooltip, Glyphicon } from 'react-bootstrap';
import { localize } from 'react-redux-localization';
import translations from './translation/data-translations.json';
import Switcher from './translation/Switcher';
import Translate from './translation/Translate';
/*
* Navigation bar Component
*/
class NavigationBar extends Component {
render() {
return (
<Navbar fixedTop>
<Navbar.Header>
<Navbar.Brand>
<NavLink to="/"><img alt="logo" src="/logo.png" className="cfn-logo" /></NavLink>
</Navbar.Brand>
<Navbar.Toggle />
</Navbar.Header>
<Navbar.Collapse>
<div className="top-navigation">
<Nav>
<NavItem eventKey={1} href="https://twitter.com/code4africa" target="_blank"><Translate value="Get_in_touch" /></NavItem>
<OverlayTrigger trigger="click" placement="bottom" overlay={<EmbedPopup />} rootClose={true}>
<NavItem eventKey={2} href="#"><Translate value="Embed_this" /> <i className="fa fa-caret-down" aria-hidden="true"></i></NavItem>
</OverlayTrigger>
<NavItem className="navitem-divider"></NavItem>
<NavItem eventKey={3} href=" https://facebook.com" target="_blank">
<i className="fa fa-facebook" aria-hidden="true"></i>
</NavItem>
<NavItem href="https://twitter.com" target="_blank">
<i className="fa fa-twitter" aria-hidden="true"></i>
</NavItem>
</Nav>
<div className="language-switcher-btns">
<Switcher />
</div>
</div>
</Navbar.Collapse>
</Navbar >
);
}
}
export default localize(translations)(NavigationBar);
And the Translate component as;
./traslation/Translate.js
import React from 'react';
import { localize } from 'react-redux-localization';
import translations from './data-translations.json';
const Translate = ({ l , value}) => (
<span>{l(value)}</span>
);
export default localize(translations)(Translate);
By calling the Translate component as <Translate value="hello" /> it will return the translated value based on the locale BUT inside <span></span> tag. I tried to remove the <span></span> in the Translate component but it does not compile.