How to add Material-ui App Bar to class component?
Im trying to add the App Bar from Material-ui (https://material-ui.com/demos/app-bar/) to my NavBar component. Material-ui examples creates a function and exports it.
class App extends Component {
render() {
return (
<div>
<NavBar />
</div>
);
}
}
The NavBar component should return the App Bar from the Material example.
class NavBar extends Component {
render() {
return (
//App Bar from material.
)
}
}
export default NavBar;
So far I have tried to create the const and use it in the navbar component render()
const styles = {
root: {
flexGrow: 1,
},
grow: {
flexGrow: 1,
},
menuButton: {
marginLeft: -12,
marginRight: 20,
},
};
class NavBar extends Component {
render() {
return (
<AppBar position="static">
<Toolbar>
<IconButton className={styles.menuButton} color="inherit" aria-label="Menu">
<MenuIcon />
</IconButton>
<Typography variant="h6" color="inherit" className={styles.grow}>
News
</Typography>
<Button color="inherit">Login</Button>
</Toolbar>
</AppBar>
)
}
}
But it doesn't render like the example one, for example the login button position its beside the News label.
reactjs react-native material-ui
add a comment |
Im trying to add the App Bar from Material-ui (https://material-ui.com/demos/app-bar/) to my NavBar component. Material-ui examples creates a function and exports it.
class App extends Component {
render() {
return (
<div>
<NavBar />
</div>
);
}
}
The NavBar component should return the App Bar from the Material example.
class NavBar extends Component {
render() {
return (
//App Bar from material.
)
}
}
export default NavBar;
So far I have tried to create the const and use it in the navbar component render()
const styles = {
root: {
flexGrow: 1,
},
grow: {
flexGrow: 1,
},
menuButton: {
marginLeft: -12,
marginRight: 20,
},
};
class NavBar extends Component {
render() {
return (
<AppBar position="static">
<Toolbar>
<IconButton className={styles.menuButton} color="inherit" aria-label="Menu">
<MenuIcon />
</IconButton>
<Typography variant="h6" color="inherit" className={styles.grow}>
News
</Typography>
<Button color="inherit">Login</Button>
</Toolbar>
</AppBar>
)
}
}
But it doesn't render like the example one, for example the login button position its beside the News label.
reactjs react-native material-ui
add a comment |
Im trying to add the App Bar from Material-ui (https://material-ui.com/demos/app-bar/) to my NavBar component. Material-ui examples creates a function and exports it.
class App extends Component {
render() {
return (
<div>
<NavBar />
</div>
);
}
}
The NavBar component should return the App Bar from the Material example.
class NavBar extends Component {
render() {
return (
//App Bar from material.
)
}
}
export default NavBar;
So far I have tried to create the const and use it in the navbar component render()
const styles = {
root: {
flexGrow: 1,
},
grow: {
flexGrow: 1,
},
menuButton: {
marginLeft: -12,
marginRight: 20,
},
};
class NavBar extends Component {
render() {
return (
<AppBar position="static">
<Toolbar>
<IconButton className={styles.menuButton} color="inherit" aria-label="Menu">
<MenuIcon />
</IconButton>
<Typography variant="h6" color="inherit" className={styles.grow}>
News
</Typography>
<Button color="inherit">Login</Button>
</Toolbar>
</AppBar>
)
}
}
But it doesn't render like the example one, for example the login button position its beside the News label.
reactjs react-native material-ui
Im trying to add the App Bar from Material-ui (https://material-ui.com/demos/app-bar/) to my NavBar component. Material-ui examples creates a function and exports it.
class App extends Component {
render() {
return (
<div>
<NavBar />
</div>
);
}
}
The NavBar component should return the App Bar from the Material example.
class NavBar extends Component {
render() {
return (
//App Bar from material.
)
}
}
export default NavBar;
So far I have tried to create the const and use it in the navbar component render()
const styles = {
root: {
flexGrow: 1,
},
grow: {
flexGrow: 1,
},
menuButton: {
marginLeft: -12,
marginRight: 20,
},
};
class NavBar extends Component {
render() {
return (
<AppBar position="static">
<Toolbar>
<IconButton className={styles.menuButton} color="inherit" aria-label="Menu">
<MenuIcon />
</IconButton>
<Typography variant="h6" color="inherit" className={styles.grow}>
News
</Typography>
<Button color="inherit">Login</Button>
</Toolbar>
</AppBar>
)
}
}
But it doesn't render like the example one, for example the login button position its beside the News label.
reactjs react-native material-ui
reactjs react-native material-ui
edited Nov 20 '18 at 0:57
Popplar
asked Nov 20 '18 at 0:46


PopplarPopplar
567
567
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
Are you wrapping your NavBar
component in the withStyles
HOC as in the Material example? It looks like you are trying to use the styles
object directly in className
which will not work as you expect. You have to pass the styles into your NavBar
component using withStyles
like:
export default withStyles(styles)(NavBar);
Then access the styles in your NavBar
from the classes
prop:
render() {
const { classes } = this.props;
return (
<AppBar position="static">
<Toolbar>
<IconButton className={classes.menuButton} color="inherit" aria-label="Menu">
<MenuIcon />
</IconButton>
<Typography variant="h6" color="inherit" className={classes.grow}>
News
</Typography>
<Button color="inherit">Login</Button>
</Toolbar>
</AppBar>
);
}
The withStyles
HOC registers your styles
object as CSS and passes the class names into the wrapped component (in this case NavBar
) so that you can use them in the render function. You can find more information about withStyles
in Material-UI here.
thanks a lot! Also thanks for the information about withStyles, checking it now!
– Popplar
Nov 20 '18 at 2:45
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53384670%2fhow-to-add-material-ui-app-bar-to-class-component%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Are you wrapping your NavBar
component in the withStyles
HOC as in the Material example? It looks like you are trying to use the styles
object directly in className
which will not work as you expect. You have to pass the styles into your NavBar
component using withStyles
like:
export default withStyles(styles)(NavBar);
Then access the styles in your NavBar
from the classes
prop:
render() {
const { classes } = this.props;
return (
<AppBar position="static">
<Toolbar>
<IconButton className={classes.menuButton} color="inherit" aria-label="Menu">
<MenuIcon />
</IconButton>
<Typography variant="h6" color="inherit" className={classes.grow}>
News
</Typography>
<Button color="inherit">Login</Button>
</Toolbar>
</AppBar>
);
}
The withStyles
HOC registers your styles
object as CSS and passes the class names into the wrapped component (in this case NavBar
) so that you can use them in the render function. You can find more information about withStyles
in Material-UI here.
thanks a lot! Also thanks for the information about withStyles, checking it now!
– Popplar
Nov 20 '18 at 2:45
add a comment |
Are you wrapping your NavBar
component in the withStyles
HOC as in the Material example? It looks like you are trying to use the styles
object directly in className
which will not work as you expect. You have to pass the styles into your NavBar
component using withStyles
like:
export default withStyles(styles)(NavBar);
Then access the styles in your NavBar
from the classes
prop:
render() {
const { classes } = this.props;
return (
<AppBar position="static">
<Toolbar>
<IconButton className={classes.menuButton} color="inherit" aria-label="Menu">
<MenuIcon />
</IconButton>
<Typography variant="h6" color="inherit" className={classes.grow}>
News
</Typography>
<Button color="inherit">Login</Button>
</Toolbar>
</AppBar>
);
}
The withStyles
HOC registers your styles
object as CSS and passes the class names into the wrapped component (in this case NavBar
) so that you can use them in the render function. You can find more information about withStyles
in Material-UI here.
thanks a lot! Also thanks for the information about withStyles, checking it now!
– Popplar
Nov 20 '18 at 2:45
add a comment |
Are you wrapping your NavBar
component in the withStyles
HOC as in the Material example? It looks like you are trying to use the styles
object directly in className
which will not work as you expect. You have to pass the styles into your NavBar
component using withStyles
like:
export default withStyles(styles)(NavBar);
Then access the styles in your NavBar
from the classes
prop:
render() {
const { classes } = this.props;
return (
<AppBar position="static">
<Toolbar>
<IconButton className={classes.menuButton} color="inherit" aria-label="Menu">
<MenuIcon />
</IconButton>
<Typography variant="h6" color="inherit" className={classes.grow}>
News
</Typography>
<Button color="inherit">Login</Button>
</Toolbar>
</AppBar>
);
}
The withStyles
HOC registers your styles
object as CSS and passes the class names into the wrapped component (in this case NavBar
) so that you can use them in the render function. You can find more information about withStyles
in Material-UI here.
Are you wrapping your NavBar
component in the withStyles
HOC as in the Material example? It looks like you are trying to use the styles
object directly in className
which will not work as you expect. You have to pass the styles into your NavBar
component using withStyles
like:
export default withStyles(styles)(NavBar);
Then access the styles in your NavBar
from the classes
prop:
render() {
const { classes } = this.props;
return (
<AppBar position="static">
<Toolbar>
<IconButton className={classes.menuButton} color="inherit" aria-label="Menu">
<MenuIcon />
</IconButton>
<Typography variant="h6" color="inherit" className={classes.grow}>
News
</Typography>
<Button color="inherit">Login</Button>
</Toolbar>
</AppBar>
);
}
The withStyles
HOC registers your styles
object as CSS and passes the class names into the wrapped component (in this case NavBar
) so that you can use them in the render function. You can find more information about withStyles
in Material-UI here.
answered Nov 20 '18 at 2:27


TylerTyler
1,00117
1,00117
thanks a lot! Also thanks for the information about withStyles, checking it now!
– Popplar
Nov 20 '18 at 2:45
add a comment |
thanks a lot! Also thanks for the information about withStyles, checking it now!
– Popplar
Nov 20 '18 at 2:45
thanks a lot! Also thanks for the information about withStyles, checking it now!
– Popplar
Nov 20 '18 at 2:45
thanks a lot! Also thanks for the information about withStyles, checking it now!
– Popplar
Nov 20 '18 at 2:45
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53384670%2fhow-to-add-material-ui-app-bar-to-class-component%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown