React Class Component not working properly for me - reactjs

I have a component of "Drawer", I am opening and closing this drawer with component state and passing this state down to the Drawer Component and Also passing a callback function that can help me to close the drawer,
Now the issue is that when ever I am trying to open that drawer, the whole ui is disappearing. Need help
here is the code
import IconButton from '#material-ui/core/IconButton';
import MenuIcon from '#material-ui/icons/Menu';
import Drawer from '../Drawer';
import Header from '../Header';
export default class LandingPage extends Component {
constructor() {
super();
this.state = {
openDrawer: false,
};
}
handleDrawer = (state) => {
this.setState({
openDrawer: state,
});
};
render() {
return (
<div className='landing-page-container'>
<div className='menu-btn'>
<IconButton
edge='start'
color='inherit'
aria-label='menu'
onClick={() => this.handleDrawer(true)}
>
<MenuIcon />
</IconButton>
</div>
<Drawer
handleDrawer={this.handleDrawer}
openDrawer={this.state.openDrawer}
/>
</div>
);
}
}
Here is Drawer
import React, { Component } from 'react';
import {
Drawer,
IconButton,
Divider,
List,
ListItem,
ListItemIcon,
Button,
ListItemText,
makeStyles,
} from '#material-ui/core';
import { withStyles } from '#material-ui/core/styles';
import {
ChevronLeft,
NoteAdd,
Person,
PersonAdd,
AttachMoney,
Build,
Settings,
} from '#material-ui/icons';
const styles = makeStyles((theme) => ({
root: {
display: 'flex',
},
drawerPaper: {
backgroundColor: theme.primary,
},
}));
export class DrawerMenu extends Component {
render() {
const { classes, openDrawer, handleDrawer } = this.props;
return (
<div className='drawer-container'>
<Drawer
variant='persistent'
anchor='left'
open={false}
classes={{
paper: classes.drawerPaper,
}}
backgroundColor='primary'
>
<div className='logo-icon'>
<div className='logo'>Logo</div>
<div className='collapse-icon'>
<IconButton onClick={() => handleDrawer(false)}>
<ChevronLeft />
</IconButton>
</div>
</div>
<Divider />
<div className='drawer-menu-container'>
<div className='drawer-menu'>
<List>
{[
'Booking',
'Positions',
'User Management',
'Trading',
'Instruments',
].map((text, index) => (
<ListItem button key={text}>
<ListItemIcon>
{index === 0 && <NoteAdd />}
{index === 1 && <Person />}
{index === 2 && <PersonAdd />}
{index === 3 && <AttachMoney />}
{index === 4 && <Build />}
</ListItemIcon>
<ListItemText primary={text} />
</ListItem>
))}
</List>
<Divider />
</div>
<div className='drawer-action-button'>
<div className='setting-logout'>
<div className='setting'>
<Settings />
</div>
<div className='logout'>
<Button variant='contained' color='primary'>
Logout
</Button>
</div>
</div>
</div>
</div>
</Drawer>
</div>
);
}
}
export default withStyles(styles, { withTheme: true })(DrawerMenu);

Your problem could be related to the fact that your Drawer is not on a Fragment. Try to modify your Drawer component code like:
import React, { Component, Fragment } from 'react'; //<-- import Fragment
...
export class DrawerMenu extends Component {
render() {
<div className='drawer-container'>
<Fragment>
<Drawer
variant='persistent'
anchor='left'
open={false}
classes={{
paper: classes.drawerPaper,
}}
backgroundColor='primary'
>
...
</Drawer>
</Fragment>
</div>
};
}
This should solve your problem.

Related

How to make a react global state re-render component?

I have tried using two different libraries to handle this issue and both time I get the same behavior.
Libraries tried: OvermindJS, Hookstate
When I click the button I can see the state change is beeing logged in the console but the component will only re-render on the second click
If I change page:
click Home
click Page1
click No Funds
Then it will show 1$
If I click straightway the No Funds button without changing page (first action on page) then the button will not re-render until it is clicked twice.
App.tsx
import * as React from "react"
import {
ChakraProvider,
Box,
Text,
Grid,
theme,
} from "#chakra-ui/react"
import { Switch, Route } from "wouter"
import { Appbar } from "./common/AppBar"
export const App = () => (
<ChakraProvider theme={theme}>
<Appbar />
<Box textAlign="center" fontSize="xl">
<Grid minH="100vh" p={3}>
<Switch>
<Route path="/">
<Text mt={100}>Home</Text>
</Route>
<Route path="/home">
<Text mt={100}>Home</Text>
</Route>
<Route path="/page1">
<Text mt={100}>Page 1</Text>
</Route>
</Switch>
</Grid>
</Box>
</ChakraProvider>
)
AppBar.tsx
import React, { ReactNode } from "react";
import {
Box,
Flex,
HStack,
Link,
IconButton,
Button,
Icon,
useDisclosure,
useColorModeValue,
Stack,
} from '#chakra-ui/react';
import { HamburgerIcon, CloseIcon } from '#chakra-ui/icons';
import { MdAccountBalanceWallet } from 'react-icons/md'
import { useLocation } from 'wouter';
import { actionIncrementFunds, globalState } from "../hookState/state";
import { useState } from "#hookstate/core";
const Links = ['Home', 'Page1'];
const NavLink: React.FC<any> = ({ children, handleClick }: { children: ReactNode, handleClick: any }) => (
<Link
px={2}
py={1}
rounded={'md'}
_hover={{
textDecoration: 'none',
bg: useColorModeValue('red.200', 'red.300'),
}}
onClick={() => handleClick(children)}>
{children}
</Link>
);
export const Appbar: React.FC = () => {
const state = useState(globalState);
const { isOpen, onOpen, onClose } = useDisclosure();
const [location, setLocation] = useLocation();
const handleClick = (path: string) => {
setLocation(`/${path.toLowerCase()}`)
}
const hasFunds = () => {
return state.currentFunds.get() > 0
}
const handleConnectWallet = () => {
actionIncrementFunds()
}
return (
<>
<Box zIndex={900} position={"fixed"} top={0} left={0} width="100%" bg={useColorModeValue('gray.100', 'gray.900')} px={4}>
<Flex h={16} alignItems={'center'} justifyContent={'space-between'}>
<IconButton
size={'md'}
icon={isOpen ? <CloseIcon /> : <HamburgerIcon />}
aria-label={'Open Menu'}
display={{ md: 'none' }}
onClick={isOpen ? onClose : onOpen}
/>
<HStack height={"100%"} spacing={8} alignItems={'center'}>
<HStack
as={'nav'}
spacing={4}
display={{ base: 'none', md: 'flex' }}>
{Links.map((link) => (
<NavLink handleClick={handleClick} key={link}>{link}</NavLink>
))}
</HStack>
</HStack>
<Flex alignItems={'center'}>
{hasFunds()
? <Button
variant={'solid'}
colorScheme={'red'}
size={'md'}
onClick={handleConnectWallet}
mr={4}>
{state.currentFunds.get()} $
</Button>
: <Button
variant={'solid'}
colorScheme={'red'}
size={'md'}
mr={4}
onClick={handleConnectWallet}
leftIcon={<Icon as={MdAccountBalanceWallet} />}>
No Funds
</Button>
}
</Flex>
</Flex>
{
isOpen ? (
<Box pb={4} display={{ md: 'none' }}>
<Stack as={'nav'} spacing={4}>
{Links.map((link) => (
<NavLink handleClick={handleClick} key={link}>{link}</NavLink>
))}
</Stack>
</Box>
) : null
}
</Box >
</>
);
}
Behavior example:
Repo with example for Hookstate:
https://github.com/crimson-med/state-issue
Repo with example for OvermindJS:
https://github.com/crimson-med/state-issue/tree/overmind
How can I get the button on change on the first click?

Menu list overlapping with another Menu list

I'm new to react.js and material UI, i'm try to make a Navigation top navbar for my project,
i use the 'MenuList' composition to my top navbar, the first one Button is success,but when i add another one and click it, it will overlapping with first menu list. Can someone give some hints? Thank you all.
here is the problem image
and here is my navbar source code
import React from 'react';
import { makeStyles } from '#material-ui/core/styles';
import Paper from '#material-ui/core/Paper';
import Tabs from '#material-ui/core/Tabs';
import Tab from '#material-ui/core/Tab';
import ClickAwayListener from '#material-ui/core/ClickAwayListener';
import Button from '#material-ui/core/Button';
import Grow from '#material-ui/core/Grow';
import Popper from '#material-ui/core/Popper';
import MenuItem from '#material-ui/core/MenuItem';
import MenuList from '#material-ui/core/MenuList';
import { Link, Route, withRouter } from 'react-router-dom';
const useStyles = makeStyles({
root: {
flexGrow: 1,
},
});
export default function CenteredTabs() {
const classes = useStyles();
const [value, setValue] = React.useState(0);
const [open, setOpen] = React.useState(false);
const anchorRef = React.useRef(null);
const handleChange = (event, newValue) => {
setValue(newValue);
};
const handleToggle = () => {
setOpen(prevOpen => !prevOpen);
};
const handleClose = event => {
if (anchorRef.current && anchorRef.current.contains(event.target)) {
return;
}
setOpen(false);
};
function handleListKeyDown(event) {
if (event.key === 'Tab') {
event.preventDefault();
setOpen(false);
}
}
// return focus to the button when we transitioned from !open -> open
const prevOpen = React.useRef(open);
React.useEffect(() => {
if (prevOpen.current === true && open === false) {
anchorRef.current.focus();
}
prevOpen.current = open;
}, [open]);
return (
<Paper className={classes.root}>
<Tabs
value={value}
onChange={handleChange}
indicatorColor="secondary"
textColor="primary"
>
<Tab label="WeniPay" to="/" component={Link} style={{ float: "left" }} />
<Tab label="Home" to="/" component={Link} />
<Tab label="Login" to="/works" component={Link} />
<Tab label="Pay" to="/payPage" component={Link} />
</Tabs>
<Button
ref={anchorRef}
aria-controls={open ? 'menu-list-grow' : undefined}
aria-haspopup="true"
onClick={handleToggle}
>
Toggle Menu Grow
</Button>
<Popper open={open} anchorEl={anchorRef.current} role={undefined} transition disablePortal>
{({ TransitionProps, placement }) => (
<Grow
{...TransitionProps}
style={{ transformOrigin: placement === 'bottom' ? 'center top' : 'center bottom' }}
>
<Paper>
<ClickAwayListener onClickAway={handleClose}>
<MenuList autoFocusItem={open} id="menu-list-grow" onKeyDown={handleListKeyDown}>
<MenuItem onClick={handleClose}>Profile</MenuItem>
<MenuItem onClick={handleClose}>My account</MenuItem>
<MenuItem onClick={handleClose}>Logout</MenuItem>
</MenuList>
</ClickAwayListener>
</Paper>
</Grow>
)}
</Popper>
<Button
ref={anchorRef}
aria-controls={open ? 'menu-list-grow' : undefined}
aria-haspopup="true"
onClick={handleToggle}
>
s
</Button>
<Popper open={open} anchorEl={anchorRef.current} role={undefined} transition disablePortal>
{({ TransitionProps, placement }) => (
<Grow
{...TransitionProps}
style={{ transformOrigin: placement === 'bottom' ? 'center top' : 'center bottom' }}
>
<Paper>
<ClickAwayListener onClickAway={handleClose}>
<MenuList autoFocusItem={open} id="menu-list-grow" onKeyDown={handleListKeyDown}>
<MenuItem onClick={handleClose}>1</MenuItem>
<MenuItem onClick={handleClose}>2</MenuItem>
<MenuItem onClick={handleClose}>3</MenuItem>
</MenuList>
</ClickAwayListener>
</Paper>
</Grow>
)}
</Popper>
</Paper>
);
}
and here is main page
import React from 'react';
import styles from './App.module.scss';
import { Link, Route, withRouter } from 'react-router-dom';
import HomePage from './HomePage';
import WorkPage from './WorkPage';
import WorkPageDetail from './WorkPageDetail';
import Header from './header';
import PayPage from './PayPage';
class App extends React.Component {
render() {
const { location } = this.props;
return (
<div className={styles.App}>
{/* header */}
<header className={styles.header}>
<div className={styles.box}>
<Header />
</div>
</header>
{/* content */}
<section className={styles.content}>
<Route path="/" exact component={HomePage} />
<Route path="/works" exact component={WorkPage} />
<Route path="/works/:id" exact component={WorkPageDetail} />
<Route path="/payPage" exact component={PayPage} />
</section>
{/* footer */}
<footer className={styles.footer}>
<p>© <b>MyPay</b></p>
</footer>
</div>
);
}
}
export default withRouter(App);
You can make archorEl as array so you can set like
// set anchor as empty list so we can track it later
const [anchorEl, setAnchorEl] = React.useState<[] | HTMLElement[]>([]));
const openMenu = (event, index) => {
const tempAnchor = anchorEl;
tempAnchor[index] = event.currentTarget; // only set change version
setAnchorEl(tempAnchor);
};
const closeMenu = () => {
setAnchorEl([]); // now we set it to empty list
}
and our tsx would be like
<Menu
id={row.id}
anchorEl={anchorEl[index]}
keepMounted
open={Boolean(anchorEl[index])}
onClose={() => closeMenu()}
>
<MenuItem> Foo</MenuItem>
<MenuItem> Bar</MenuItem>
</Menu>

Button not redirecting to new page in ReactJS

Earlier I faced with a syntax error in GpDialog.js, which was solved using a fix from here. However, even when the code compiles, the button doesn't work as expected, even though the console.log value is true, meaning that there should not be an exception thrown. Does anyone know a fix to make the redirection work as expected? Here's some of the relevant code, feel free to ask for more or a clarification.
GpDialog.js
import React, { Component } from "react";
import Dialog from "#material-ui/core/Dialog";
import { Link } from "react-router-dom";
import Button from "#material-ui/core/Button";
import Grid from "#material-ui/core/Grid";
import {
DialogActions,
DialogContent,
DialogContentText,
DialogTitle
} from "#material-ui/core";
export class GpDialog extends Component {
state = {
open: false
};
handleToggle = () => {
this.setState({
open: !this.state.open
});
};
render() {
const { onClose, selectedGP, ...other } = this.props;
const { open } = this.state;
const { clinic } = this.props;
const handleToggle = () => {
this.setState({
open: !this.state.open
});
};
function handleClose() {
onClose(selectedGP);
}
function handleListItemClick(clinic, name) {
onClose(clinic, name);
handleToggle();
}
return (
<div>
<Button variant="outlined" fullWidth="true" onClick={this.handleToggle}>
{clinic.properties.HCI_NAME}
</Button>
<Dialog open={open} onClose={handleToggle}>
<DialogContent>
Clinic Name: {clinic.properties.HCI_NAME} <hr /> Address:{" "}
{clinic.properties.BLK_HSE_NO} {clinic.properties.STREET_NAME} #
{clinic.properties.FLOOR_NO}-{clinic.properties.UNIT_NO}{" "}
{clinic.properties.BUILDING_NAME} Singapore{" "}
{clinic.properties.PostalCode}
<hr /> Telephone: {clinic.properties.Tel} <hr />
Applicable subsidies:{" "}
{clinic.properties.CLINIC_PROGRAMME_CODE.join(", ")}
<hr />
Distance:
{parseFloat(clinic.distance).toFixed(2)}km away
<hr />
<Grid style={{ flexGrow: 1 }} direction="row">
<Grid container justify="space-between">
<Grid item>
<Button
variant="contained"
color="primary"
onClick={() =>
handleListItemClick(clinic, clinic.properties.HCI_NAME)
}
>
<span style={{ color: "white" }}>Add to comparison</span>
</Button>
</Grid>
<Grid item>
<Button
variant="contained"
style={{ backgroundColor: "#ff7c01" }}
onClick={this.handleCompare}
>
<Link
to={{
pathname: "/ConfirmClinicChoice",
state: {
choice: clinic,
formData: this.props.formData
}
}}
>
<span style={{ color: "white" }}>Select</span>
</Link>
</Button>
</Grid>
<Grid item>
<Button
variant="contained"
style={{ backgroundColor: "#ff7c01" }}
// cannot really break out of the scope, link must be binded with a button
onClick={() => {
const files = ["67690636.jpeg"];
console.log(
files.includes(`${clinic.properties.Tel}.jpeg`)
);
if (!files.includes(`${clinic.properties.Tel}.jpeg`)) {
alert(
"No pictorial information based on current dataset"
);
return;
}
return (
<Link
to={{
pathname: "/PcnImagePage",
state: {
choice: clinic
}
}}
></Link>
);
}}
>
<span style={{ color: "white" }}>More info</span>
</Button>
</Grid>
</Grid>
</Grid>
</DialogContent>
</Dialog>
</div>
);
}
}
export default GpDialog;
PcnImagePage.js
import React, { Component } from "react";
import PCRoute from "../images/DischargeRoutes/PolyclinicRoute.png";
import Paper from "#material-ui/core/Paper";
import AppBar from "#material-ui/core/AppBar";
import Toolbar from "#material-ui/core/Toolbar";
import IconButton from "#material-ui/core/IconButton";
import ArrowBack from "#material-ui/icons/ArrowBackIos";
import { Typography, Button, Card } from "#material-ui/core";
import { makeStyles } from "#material-ui/core/styles";
const useStyles = makeStyles(theme => ({
root: {
padding: theme.spacing(3, 3)
}
}));
export const PcnImagePage = props => {
const classes = useStyles();
function goBack() {
props.history.goBack();
}
const { choice } = props.location.state;
const result = (
<Paper
square="false"
className={classes.root}
style={{ fontWeight: "bold" }}
>
<div>
<span>More information </span>
<img src={PCRoute} alt="pc route" style={{ width: "100%" }} />
</div>
</Paper>
);
return (
<div>
<AppBar position="static" style={{ backgroundColor: "#ff7c01" }}>
<Toolbar>
<IconButton
edge="start"
color="inherit"
aria-label="menu"
onClick={goBack}
>
<ArrowBack />
<Typography variant="subtitle1">Back to views</Typography>
</IconButton>{" "}
<Typography variant="h5" align="center" style={{ flexGrow: 1 }}>
More information
</Typography>
<Typography variant="subtitle1">
<span style={{ color: "#ff7c01" }}>----------------</span>
</Typography>
</Toolbar>
</AppBar>
{result}
<br />
<br />
</div>
);
};
export default PcnImagePage;
App.js
import React from "react";
import Login from "./pages/Welcome";
import { BrowserRouter as Router, Switch, Route } from "react-router-dom";
import Language from "./pages/Language";
import GeneralInfo from "./pages/GeneralInfo";
import Form from "./pages/PatientForm";
import FilteredResult from "./pages/FilteredResult";
import ConfirmClinicChoicePage from "./pages/ConfirmClinicChoice";
import confirmedChoicePage from "./pages/SummaryPage";
import PcnImagePage from "./pages/PcnImagePage";
class App extends React.Component {
render() {
return (
<Router>
<div>
<Switch>
<Route path="/" exact component={Login} />
<Route path="/Language" exact component={Language} />
<Route path="/GeneralInfo" exact component={GeneralInfo} />
<Route path="/Form" exact component={Form} />
<Route path="/FilteredResult" exact component={FilteredResult} />
<Route
path="/ConfirmClinicChoice"
exact
component={ConfirmClinicChoicePage}
/>
<Route
path="/confirmedChoice"
exact
component={confirmedChoicePage}
/>
<Route path="/PcnImagePage" exact component={PcnImagePage} />
</Switch>
</div>
</Router>
);
}
}
export default App;
Update
I have tried suggestions to use Redirect instead, and the only changes are the code snippet on GpDialog.js. This is the change, other than the inclusion of 1 more import statement.
return (<Redirect
to={{
pathname: "/PcnImagePage",
state: {
choice: clinic
}
}}
/>
)
}
}
>
<span style={{ color: "white" }}>More info</span>
</Button>
</Grid>
</Grid>
</Grid>
</DialogContent>
</Dialog>
</div>
);
}
}
Returning component in your onClick event handler won't work as desired. In your case I would recommend to use component's state and Redirect component.
To redirect to a new page set state's redirectTo object to desired value. It will later render Redirect component with redirectTo object passed to to property, which will programmatically navigate to a specified location.
GpDialog.js
import React, { Component } from "react";
import Dialog from "#material-ui/core/Dialog";
import { Link, Redirect } from "react-router-dom";
import Button from "#material-ui/core/Button";
import Grid from "#material-ui/core/Grid";
import {
DialogActions,
DialogContent,
DialogContentText,
DialogTitle
} from "#material-ui/core";
export class GpDialog extends Component {
state = {
open: false,
redirectTo: null
};
handleToggle = () => {
this.setState({
open: !this.state.open
});
};
render() {
const { onClose, selectedGP, ...other } = this.props;
const { open } = this.state;
const { clinic } = this.props;
const handleToggle = () => {
this.setState({
open: !this.state.open
});
};
function handleClose() {
onClose(selectedGP);
}
function handleListItemClick(clinic, name) {
onClose(clinic, name);
handleToggle();
}
if (this.state.redirectTo) {
return (
<Redirect to={this.state.redirectTo} />
);
}
return (
<div>
<Button variant="outlined" fullWidth="true" onClick={this.handleToggle}>
{clinic.properties.HCI_NAME}
</Button>
<Dialog open={open} onClose={handleToggle}>
<DialogContent>
Clinic Name: {clinic.properties.HCI_NAME} <hr /> Address:{" "}
{clinic.properties.BLK_HSE_NO} {clinic.properties.STREET_NAME} #
{clinic.properties.FLOOR_NO}-{clinic.properties.UNIT_NO}{" "}
{clinic.properties.BUILDING_NAME} Singapore{" "}
{clinic.properties.PostalCode}
<hr /> Telephone: {clinic.properties.Tel} <hr />
Applicable subsidies:{" "}
{clinic.properties.CLINIC_PROGRAMME_CODE.join(", ")}
<hr />
Distance:
{parseFloat(clinic.distance).toFixed(2)}km away
<hr />
<Grid style={{ flexGrow: 1 }} direction="row">
<Grid container justify="space-between">
<Grid item>
<Button
variant="contained"
color="primary"
onClick={() =>
handleListItemClick(clinic, clinic.properties.HCI_NAME)
}
>
<span style={{ color: "white" }}>Add to comparison</span>
</Button>
</Grid>
<Grid item>
<Button
variant="contained"
style={{ backgroundColor: "#ff7c01" }}
onClick={this.handleCompare}
>
<Link
to={{
pathname: "/ConfirmClinicChoice",
state: {
choice: clinic,
formData: this.props.formData
}
}}
>
<span style={{ color: "white" }}>Select</span>
</Link>
</Button>
</Grid>
<Grid item>
<Button
variant="contained"
style={{ backgroundColor: "#ff7c01" }}
// cannot really break out of the scope, link must be binded with a button
onClick={() => {
const files = ["67690636.jpeg"];
console.log(
files.includes(`${clinic.properties.Tel}.jpeg`)
);
if (!files.includes(`${clinic.properties.Tel}.jpeg`)) {
alert(
"No pictorial information based on current dataset"
);
return;
}
this.setState({
redirectTo: {
pathname: "/PcnImagePage",
state: {
choice: clinic
}
}
});
}}
>
<span style={{ color: "white" }}>More info</span>
</Button>
</Grid>
</Grid>
</Grid>
</DialogContent>
</Dialog>
</div>
);
}
}
export default GpDialog;
Before edit:
A Link component creates a link element, which still requires an user to click it before navigating to a new page. But there is also a Redirect component, which, when rendered, instantly navigates to a different location. Therefore in your case in GpDialog.js file you should use Redirect component instead of Link.
Check out the official API docs: https://reacttraining.com/react-router/web/api/Redirect

Edit component inside Drawer component

I am using react-admin framework and I am trying to put the <Edit> component inside <Drawer> component. I need this in order to be able to save changes done in my JsonInput.
This is my custom component:
import React, { Component, Fragment } from 'react';
import { fetchUtils, CardActions, EditButton, Button, List, Datagrid, Edit } from 'react-admin';
import Drawer from '#material-ui/core/Drawer';
import JsonInput from 'react-json-view';
import EditIcon from '#material-ui/icons/Edit';
import IconKeyboardArrowRight from '#material-ui/icons/KeyboardArrowRight';
import { SimpleForm } from 'ra-ui-materialui/lib/form';
const divStyle = {
width: '400px',
margin: '1em'
};
export default class JsonEditButton extends React.Component {
constructor(props, context) {
super(props, context);
this.state = { showPanel: false };
}
handleClick = () => {
this.setState({ showPanel: true });
};
handleCloseClick = () => {
this.setState({ showPanel: false });
};
onChange = (value) => {
const { updated_src, name, namespace, new_value, existing_value } = value;
//this.onChange(updated_src);
}
render() {
const { showPanel } = this.state;
return (
<div>
<Button label="Upravit JSON" onClick={this.handleClick}>
<EditIcon />
</Button>
<Fragment>
<Drawer
anchor="right"
open={showPanel}
onClose={this.handleCloseClick}
>
<div>
<Button label="Zavřít" onClick={this.handleCloseClick}>
<IconKeyboardArrowRight />
</Button>
</div>
<div style={divStyle}>
{props => (
<Edit {...props}>
<SimpleForm>
{this.props.record && <JsonInput src={this.props.record} name={null} displayDataTypes={false} displayObjectSize={false} onEdit={this.onChange} onAdd={this.onChange} onDelete={this.onChange} />}
</SimpleForm>
</Edit>
)}
</div>
</Drawer>
</Fragment>
</div>
);
}
};
However this doesnt return any HTML.
Any idea how to solve this?
Thank you in advance.
Maybe this may help
<div style={divStyle}>
<Edit {...this.props}>
<SimpleForm>
{this.props.record && <JsonInput src={this.props.record} name={null} displayDataTypes={false} displayObjectSize={false} onEdit={this.onChange} onAdd={this.onChange} onDelete={this.onChange} />}
</SimpleForm>
</Edit>
</div>
the previous code where {props => ()} is actually a function or you could try this
<div style={divStyle}>
{props => (
return(
<Edit {...props}>
<SimpleForm>
{this.props.record && <JsonInput src={this.props.record} name={null} displayDataTypes={false} displayObjectSize={false} onEdit={this.onChange} onAdd={this.onChange} onDelete={this.onChange} />}
</SimpleForm>
</Edit>)
)}
</div>

Render components on Material UI tabs

I have been trying to render certain components on clicking inside a tab content.
This is what i have been doing:
import React, { Component } from "react";
export class Global extends Component {
render() {
return <div>Global</div>;
}
}
export default Global;
This is a basic component to show.
import React, { Component } from "react";
import PropTypes from "prop-types";
import AppBar from "#material-ui/core/AppBar";
import Tabs from "#material-ui/core/Tabs";
import Tab from "#material-ui/core/Tab";
import Typography from "#material-ui/core/Typography";
import Toolbar from "#material-ui/core/Toolbar";
import Button from "#material-ui/core/Button";
import { BrowserRouter, Switch, Route } from "react-router-dom";
import IconButton from "#material-ui/core/IconButton";
import SearchIcon from "#material-ui/icons/Search";
import Link from "#material-ui/core/Link";
import Global from "../Dashboard/Global";
import Graficos from "../Dashboard/Graficos";
import Individual from "../Dashboard/Individual";
import Temporais from "../Dashboard/Temporais";
import CSSTransitionGroup from "react-addons-css-transition-group";
import { Paper } from "#material-ui/core";
import { classes } from "../constants/tabs";
function TabContainer(props) {
return (
<Typography
component="div"
style={{
padding: 8 * 3
}}
>
{" "}
{props.children}{" "}
</Typography>
);
}
TabContainer.propTypes = {
children: PropTypes.node.isRequired
};
//const sections = ["Indicador 1", "Indicador 2", "Indicador 2", "Indicador 2"];
const PageShell = (Page, previous) => {
return props => (
<div className="page">
<CSSTransitionGroup
transitionAppear={true}
transitionAppearTimeout={600}
transitionEnterTimeout={600}
transitionLeaveTimeout={600}
transitionName={props.match.path === "/one" ? "SlideIn" : "SlideOut"}
>
{console.log(props)}
<Page {...props} />
</CSSTransitionGroup>
</div>
);
};
function ItemOne(theme) {
return (
<Paper>
<div>Item 1</div>
</Paper>
);
}
function ItemTwo(theme) {
return (
<Paper>
<div>Item two</div>
</Paper>
);
}
export default function Header() {
const [value, setValue] = React.useState(0);
function handleChange(event, newValue) {
setValue(newValue);
}
return (
<div className={classes.root}>
<AppBar position="static">
<Tabs value={value} onChange={handleChange}>
{/* <Tabs value={value} onChange={handleChange}>
{sections.map(section => (
<Tab label={section} />
))}
</Tabs> */}
<Tab label="Item One" component={Link} to="/global" />
<Tab label="Item Two" component={Link} to="/individual" />
<Tab label="Item Three" component={Link} to="/temporal" />
<Tab label="Item Four" component={Link} to="/graficos" />
</Tabs>
</AppBar>
<Switch>
<Route path="/global" component={PageShell(ItemOne)} />
<Route path="/individual" component={PageShell(ItemTwo)} />
</Switch>
{/* {value === 0 && <TabContainer>Item One</TabContainer>}
{value === 1 && <TabContainer>Item Two</TabContainer>}
{value === 2 && <TabContainer>Item Three</TabContainer>}
{value === 3 && <TabContainer>Item Four</TabContainer>} */}
</div>
);
}
This is my material header and im trying to load that global component below my header. The idea is that my header is fixed and the component renders on tab click. Can anyone help me with what im doing wrong ?

Resources