sidebar with Reactjs - reactjs

I am trying to do a sidebar with Reactjs but I am not getting it.
I want to know how to pass the props from App.js to Middle.js correctly.
I have the structure index.js > routes.js > App.js > Header.js, Middle.js > Sidebar.js, (DashboardPage.js, AccountPage.js - pages to be rendered dinamically)
Header.js -> Has a IndexLink at image
Sidebar.js -> Has a IndexLink and a Link to render AccountPage.js
The route is set for App.js, but the component is supposed to load inside the Middle.js component.
Here are my codes:
index.js
import 'babel-polyfill';
import React from 'react';
import { render } from 'react-dom';
import { Router, browserHistory } from 'react-router';
import routes from './routes';
render(
<Router history={browserHistory} routes={routes} />, document.getElementById('app')
);
routes.js
import React from 'react';
import { Route, IndexRoute } from 'react-router';
import App from './components/App';
import DashboardPage from './components/middle/dashboard/DashboardPage';
import AccountPage from './components/middle/accounts/AccountPage';
export default (
<Route path="/" component={App}>
<IndexRoute component={DashboardPage} />
<Route path="accounts" component={AccountPage} />
</Route>
);
App.js
import 'babel-polyfill';
import React, {PropTypes} from 'react';
import ReactDOM from 'react-dom';
import Header from './common/Header';
import Middle from './middle/Middle'
import '../../css/style.css';
class App extends React.Component{
var { app } = this.props;
render() {
return (
<div>
<Header />
<Middle />
</div>
);
}
}
export default App;
Header.js // IndexLink at img
'use strict';
import React from 'react';
import User from './User';
import {IndexLink, Link} from 'react-router';
import '../../../css/header.css';
class Header extends React.Component{
render() {
return (
<div className="contactto-header">
<div className="contactto-header-content">
<IndexLink to="/"><img className="contactto-header-content-logo" src="static/img/logo.png" alt="contactto logo" /></IndexLink>
<div className="contactto-header-content-alarm"></div>
<div className="contactto-header-content-user">
<User />
</div>
</div>
</div>
);
}
}
export default Header;
Middle.js
'use strict';
import React, {PropTypes} from 'react';
import '../../../css/middle.css';
import SideBar from './SideBar'
class Middle extends React.Component{
render() {
return (
<div className="contactto-middle">
<div className="contactto-middle-content">
<SideBar />
{app.children} // should render here
</div>
</div>
);
}
}
export default Middle;
Sidebar.js
'use strict';
import React from 'react';
import {IndexLink, Link} from 'react-router';
import '../../../css/sidebar.css';
class SideBar extends React.Component{
render() {
return (
<div className="sidebar-container">
<ul className="sidebar-container-ul">
<li className="sidebar-container-ul-li">
<IndexLink className="sidebar-container-ul-li-a" to="/">Dashboard</IndexLink>
</li>
<li className="sidebar-container-ul-li">
<Link className="sidebar-container-ul-li-a" to="accounts">Contas</Link>
</li>
</ul>
</div>
);
}
}
export default SideBar;
What is wrong?
I am new with React, if another code is not done correctly please tell me :)
Thanks in advance.

First of all, your route components will be available under App through this.props.children. If you want to wrap them with you can try like this:
class App extends React.Component{
render() {
return (
<div>
<Header />
<Middle>
{this.props.children}
</Middle>
</div>
);
}
}
An then in the Middle.js
class Middle extends React.Component{
render() {
return (
<div className="contactto-middle">
<div className="contactto-middle-content">
<SideBar />
{this.props.children}
</div>
</div>
);
}
}

It's a bit unclear what you're asking, but it seems that you want to know how to pass a prop from App.js to Middle.js. Here's how:
class App extends React.Component{
var { app } = this.props;
render() {
return (
<div>
<Header />
<Middle someProp={someValue} />
</div>
);
}
}
Now in Middle.js you can access someProp in this.props.someProp.

Maybe you can use https://www.npmjs.com/package/react-side-bar. There are a complete example in Example and looks fine.
npm install --save react-side-bar

Related

React: Unable to render component

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';

Source component keeps rendering on other website pages when routing to them

My source component (App.js) is what I'm using as my "homepage" to route to the other pages. However when the user clicks a button that routes them to another page, the App.js componennts still renders on that page. How do I stop App.js from rendering on every page?
App.js:
import React from "react";
import { BrowserRouter as Router, Switch, Route } from "react-router-dom";
import { Component } from 'react'
import './App.css';
import 'primereact/resources/themes/nova-light/theme.css';
import 'primereact/resources/primereact.min.css';
import 'primeicons/primeicons.css';
import TruckSetupPage from "./Components/TruckSetupPage";
import carThirdPage from './Components/carThirdPage';
import { Button } from 'primereact/button';
import logoImage from './Components/corvette.png';
export class App extends Component {
render() {
return (
<Router>
<div>
<div className="Page-Title">
<div className="Page-Subtitle">
<header>car</header>
</div>
</div>
{/*Makeshift spacer because to prevent css page-title css overriding subtitle css */}
<div>
<h1 className="Page-Header-Three">corvette Online Truck Estimator</h1>
</div>
<div>
{/*Route To different pages for testing sake*/}
<center>
<switch>
<Route path="/TruckSetupPage/" component={TruckSetupPage} />
<Route path="/carThirdPage/" component={carThirdPage} />
</switch>
<GetStartedButton></GetStartedButton>
</center>
</div>
</div>
</Router>
);
}
}
export default App;
export class GetStartedButton extends Component {
getStartedClick() {
//Open new window to TruckSetup
window.open('http://localhost:3000/TruckSetupPage');
}
render() {
return (
<div>
<img src={logoImage} className="logoImage" alt="logo" />
<div>
<Button label="Get Started" className="p-button-raised" p-button-rounded onClick={this.getStartedClick} />
</div>
</div>
)
}
}
I am using the prime react framework and help is appreciated!
You need to separate the markup for your App.js in a separate component. Otherwise, the content rendered inside the Router that is not defined in a Route will display at all times.
Try something like this:
import React from "react";
import { BrowserRouter as Router, Switch, Route } from "react-router-dom";
import { Component } from 'react'
import './App.css';
import 'primereact/resources/themes/nova-light/theme.css';
import 'primereact/resources/primereact.min.css';
import 'primeicons/primeicons.css';
import TruckSetupPage from "./Components/TruckSetupPage";
import carThirdPage from './Components/carThirdPage';
import Home from "./Components/Home
export class App extends Component {
render() {
return (
<Router>
<div>
<div>
{/*Route To different pages for testing sake*/}
<center>
<switch>
<Route path="/" exact component={Home}/>
<Route path="/TruckSetupPage/" component={TruckSetupPage} />
<Route path="/carThirdPage/" component={carThirdPage} />
</switch>
</center>
</div>
</div>
</Router>
);
}
}
export default App;
Home.js
import React from "react"
import GetStartedButton from "./GetStartedButton"
const Home = () => {
return(
<div>
<div className="Page-Title">
<div className="Page-Subtitle">
<header>car</header>
</div>
</div>
{/*Makeshift spacer because to prevent css page-title css overriding subtitle css */}
<div>
<h1 className="Page-Header-Three">corvette Online Truck Estimator</h1>
</div>
<GetStartedButton/>
</div>
)
}
export default Home
GetStartedButton
import React from "react"
import { Button } from 'primereact/button';
import logoImage from './corvette.png';
const GetStartedButton = () => {
const getStartedClick = () => {
//Open new window to TruckSetup
window.open('http://localhost:3000/TruckSetupPage');
}
return (
<div>
<img src={logoImage} className="logoImage" alt="logo" />
<div>
<Button label="Get Started" className="p-button-raised" p-button-rounded onClick={getStartedClick} />
</div>
</div>
)
}
export default GetStartedButton

<Link> is not sending to the proper page with react-router-dom

I'm trying to build my first website with react, and I've had a lot of trouble with navigation. I'm using react-router and for some reason my tag is changing the url at the top of the page, but not rendering the proper component.
Here is my navbar.jsx:
import React, { Component } from "react";
import { Link, BrowserRouter } from "react-router-dom";
class Navbar extends Component {
render() {
return (
<BrowserRouter>
<div>
<ul>
<li><Link to={"/test"}>Test</Link></li>
</ul>
</div>
</BrowserRouter>
)
}
}
export default Navbar;
Here is test.jsx:
import React, { Component } from "react";
class Test extends Component {
state = {};
render() {
return (
<div>
<h1>Test</h1>
</div>
);
}
}
export default Test;
Does anyone know why this is changing the url(as expected), but not rendering test.jsx?
You need to use Route to render component for different routes.
import React, { Component } from 'react';
import { Link, BrowserRouter, Route, Switch, Redirect } from 'react-router-dom';
import Test from './import-path-of-test-component';
class Navbar extends Component {
render() {
return (
<BrowserRouter>
<div>
<ul>
<li>
<Link to={'/test'}>Test</Link>
</li>
</ul>
<Switch>
<Route exact path="/test" component={Test} />
</Switch>
</div>
</BrowserRouter>
);
}
}
export default Navbar;

Router only have one child component error

I want to create a page in such a way that it will route in such a way....... that
App.js to HomePage.js to Footer.js. Now when I try to perform this task it shows error that Router has only 1 child element.
Code for App.js:
import React, { Component } from 'react';
import logo from './logo.svg';
import HomePage from './Home_page'
import './App.css';
class App extends Component {
render() {
return (
<div className="App">
<header className="App-header">
<img src={logo} className="App-logo" alt="logo" />
<h1 className="App-title">Welcome to React</h1>
</header>
<p className="App-intro">
To get started, edit <code>src/App.js</code> and save to reload.
</p>
<HomePage />
</div>
);
}
}
export default App;
Code for Home_page.js
import React, {Component} from 'react';
import { BrowserRouter as Router, Switch, Route, Link } from 'react-router-dom';
import FooterPage from './FooterPage';
class HomePage extends Component {
render(){
return(
<Router>
<div>
<Link to='./FooterPage'>Footer Page</Link>
</div>
<Switch>
<Route exact path='./FooterPage' component={FooterPage} />
</Switch>
</Router>
);
}
}
export default HomePage;
Code for Footer.js
import React, {Component} from 'react';
import { BrowserRouter as Router, Switch, Route, Link } from 'react-router-dom';
class FooterPage extends Component {
render(){
return(
<div>
</div>
);
}
}
export default FooterPage;
At your Home_Page.js
import React, {Component} from 'react';
import { BrowserRouter as Router, Switch, Route, Link } from 'react-router-dom';
import FooterPage from './FooterPage';
class HomePage extends Component {
render(){
return(
<Router>
<div>
<div>
<Link to='./FooterPage'>Footer Page</Link>
</div>
<Switch>
<Route exact path='./FooterPage' component={FooterPage} />
</Switch>
</div>
</Router>
);
}
}
export default HomePage;
here you have only one child inside Route, not two. See https://github.com/ReactTraining/react-router/blob/master/packages/react-router-dom/docs/api/BrowserRouter.md

list transition for react isnt showing due to an error

Im New to react.
Im trying to implement this into my react app.
https://github.com/trungdq88/react-router-page-transition/blob/master/EXAMPLES.md
Now i dont want it to show on my home page like the example but in my "MyStories" component when you navigate to that component . But it seems i did something wrong and now im recieving this error.
Uncaught Error: MyStories.render(): A valid React element (or null) must be returned. You may have returned undefined, an array or some other invalid object.
heres my code.
import React from 'react';
import { Router, Route, browserHistory } from 'react-router';
import Layout from './components/Layout';
import Profile from './components/Profile';
import MyStories from './components/MyStories';
import NewStory from './components/NewStory';
import PageTransition from 'react-router-page-transition';
import DetailPage from './components/DetailPage';
class Root extends React.Component {
constructor(props){
super(props)
this.state = {
}
}
render() {
return(
<Router history={browserHistory}>
<Route path='/' component={Layout}>
<IndexRoute component={Layout} />
<Route path="/MyStories" component={MyStories}/>
<Route path="/profile" component={Profile}/>
<Route path="/NewStory" component={NewStory}/>
<Route path="/detail/:itemId" component={DetailPage} />
</Route>
<Route path="/detail/:itemId" component={DetailPage} />
</Router>
)
}
}
export default Root;
import 'babel-polyfill';
import React from 'react';
import ReactDOM from 'react-dom';
import { browserHistory } from 'react-router';
import Root from './Root';
let reactAppRender = (element) => {
ReactDOM.render(
<Root browserHistory={browserHistory}/>,
element
);
};
$(function() {
let reactApp = document.getElementById('app');
if (reactApp) {
reactAppRender(reactApp);
}
});
import React from 'react'
import { Link } from 'react-router';
export default React.createClass({
render() {
return
<div className="transition-item list-page">
{this.state.items.map(item => (
<Link
key={item.id}
className="list-item"
to={`/detail/${item.id}`}
>
<Item {...item} />
</Link>
))}
</div>
}
})
import React from 'react';
import { Link } from 'react-router';
import PageTransition from 'react-router-page-transition';
class Layout extends React.Component {
constructor(props){
super(props)
this.state = {
}
}
render() {
return(
<div>
<h1>Welcome </h1>
<ul role="nav">
<li><Link to='/'>HOME</Link></li>
<li><Link to="/profile">Profile</Link></li>
<li><Link to="/mystories">MyStories</Link></li>
<li><Link to="/newstory">NewStory</Link></li>
</ul>
<PageTransition>
{this.props.children}
</PageTransition>
</div>
)
}
}
export default Layout;
import React from 'react';
import { Link } from 'react-router';
export default class DetailPage extends React.Component {
render() {
return (
<div className="transition-item detail-page">
<Link to="/">Back</Link>
<h1>
Detail {this.props.params.itemId}
</h1>
</div>
);
}
}
The component above Layout has a render() method with a return in one line, but what you want to return starts in the line below it:
import React from 'react'
import { Link } from 'react-router';
export default React.createClass({
render() {
return
<div className="transition-item list-page">
{this.state.items.map(item => (
<Link
key={item.id}
className="list-item"
to={`/detail/${item.id}`}
>
<Item {...item} />
</Link>
))}
</div>
}
})
When this happens, wrap the JSX component in parenthesis:
import React from 'react'
import { Link } from 'react-router';
export default React.createClass({
render() {
return (
<div className="transition-item list-page">
{this.state.items.map(item => ( //Incidentally, you reference this.state here but your component has no state
<Link
key={item.id}
className="list-item"
to={`/detail/${item.id}`}
>
<Item {...item} />
</Link>
))}
</div>
)
}
})
You did this in the other components, you might have just missed this one.
For the future: if you get an error along the lines of Uncaught Error: MyStories.render(): A valid React element (or null) must be returned. look at return statement of the render method of the component that the error is mentioning (in this case, MyStories). Remember, for now you can only return JSX from the render method of a React component.

Resources