![](/img/trans.png)
[英]How do I add React component on button click in Data Grid Material-UI?
[英]How do I add AppBar with Back to Top button from Material UI for React in my App?
在 Material UI 的文檔中,我發現了這段代碼:
import React from 'react';
import PropTypes from 'prop-types';
import AppBar from '@material-ui/core/AppBar';
import Toolbar from '@material-ui/core/Toolbar';
import Typography from '@material-ui/core/Typography';
import { makeStyles } from '@material-ui/styles';
import CssBaseline from '@material-ui/core/CssBaseline';
import useScrollTrigger from '@material-ui/core/useScrollTrigger';
import Box from '@material-ui/core/Box';
import Container from '@material-ui/core/Container';
import Fab from '@material-ui/core/Fab';
import KeyboardArrowUpIcon from '@material-ui/icons/KeyboardArrowUp';
import Zoom from '@material-ui/core/Zoom';
const useStyles = makeStyles((theme) => ({
root: {
position: 'fixed',
bottom: theme.spacing(2),
right: theme.spacing(2),
},
}));
function ScrollTop(children) {
const classes = useStyles();
const trigger = useScrollTrigger({
target: window ? window() : undefined,
disableHysteresis: true,
threshold: 100
});
const handleClick = (event) => {
const anchor = (event.target.ownerDocument || document).querySelector('#back-to-top-anchor');
if (anchor) {
anchor.scrollIntoView({ behavior: 'smooth', block: 'center' });
}
};
return (<Zoom in={trigger}>
<div onClick={handleClick} role="presetation" className={classes.root}>
{children}
</div>
</Zoom>);
}
export default function BackToTop(children) {
return (
<React.Fragment>
<CssBaseline />
<AppBar>
<Toolbar>
<Typography variant="h6">Scroll to see button</Typography>
</Toolbar>
</AppBar>
<Toolbar id="back-to-top-anchor" />
<Container>
<Box my={2}>
{[...new Array(100)]
.map(
() => `Cras mattis consectetur purus sit amet fermentum.
Cras justo odio, dapibus ac facilisis in, egestas eget quam.
Morbi leo risus, porta ac consectetur ac, vestibulum at eros.
Praesent commodo cursus magna, vel scelerisque nisl consectetur et.`,
)
.join('\n')}
</Box>
</Container>
<ScrollTop {...children}>
<Fab color="secondary" size="small" aria-label="scroll back to top">
<KeyboardArrowUpIcon />
</Fab>
</ScrollTop>
</React.Fragment>
);
}
有人說這段代碼是top bar,會畫Back to top
button。 但我不明白我應該把什么作為children
傳遞給BackToTop
function。 誰能幫我?
PS Code 還包含內容,存儲在Box
元素中,僅用於測試。 如果我理解children
參數的目的,我應該有能力使BackToTop
function 獨立於任何參數。
您所指的文檔部分在這里: https://material-ui.com/components/app-bar/#back-to-top 。 該演示的代碼沙盒版本在這里: https://codesandbox.io/s/r59zg?file=/demo.js 。
與文檔中的演示相比,您問題中的代碼有一些有問題的更改。 令人困惑的是,您在幾個地方( BackToTop
參數和ScrollTop
參數)將props
重命名為children
級。 文檔代碼中需要注意的一件事是,傳遞給BackToTop
的道具從未使用過——它們是通過<ScrollTop {...props}>
傳遞給ScrollTop
的,但ScrollTop
也不使用任何這些道具(並且由於index.js
沒有將任何道具傳遞給BackToTop
,它是一個空的 object 所以沒有什么可以做的)。
這是一個稍微簡化的演示版本,用於刪除未使用的道具:
import React from "react";
import PropTypes from "prop-types";
import AppBar from "@material-ui/core/AppBar";
import Toolbar from "@material-ui/core/Toolbar";
import Typography from "@material-ui/core/Typography";
import { makeStyles } from "@material-ui/core/styles";
import CssBaseline from "@material-ui/core/CssBaseline";
import useScrollTrigger from "@material-ui/core/useScrollTrigger";
import Box from "@material-ui/core/Box";
import Container from "@material-ui/core/Container";
import Fab from "@material-ui/core/Fab";
import KeyboardArrowUpIcon from "@material-ui/icons/KeyboardArrowUp";
import Zoom from "@material-ui/core/Zoom";
const useStyles = makeStyles(theme => ({
root: {
position: "fixed",
bottom: theme.spacing(2),
right: theme.spacing(2)
}
}));
function ScrollTop(props) {
const { children } = props;
const classes = useStyles();
const trigger = useScrollTrigger({
disableHysteresis: true,
threshold: 100
});
const handleClick = event => {
const anchor = (event.target.ownerDocument || document).querySelector(
"#back-to-top-anchor"
);
if (anchor) {
anchor.scrollIntoView({ behavior: "smooth", block: "center" });
}
};
return (
<Zoom in={trigger}>
<div onClick={handleClick} role="presentation" className={classes.root}>
{children}
</div>
</Zoom>
);
}
ScrollTop.propTypes = {
children: PropTypes.element.isRequired
};
export default function BackToTop() {
return (
<React.Fragment>
<CssBaseline />
<AppBar>
<Toolbar>
<Typography variant="h6">Scroll to see button</Typography>
</Toolbar>
</AppBar>
<Toolbar id="back-to-top-anchor" />
<Container>
<Box my={2}>
{[...new Array(25)]
.map(
() => `Cras mattis consectetur purus sit amet fermentum.
Cras justo odio, dapibus ac facilisis in, egestas eget quam.
Morbi leo risus, porta ac consectetur ac, vestibulum at eros.
Praesent commodo cursus magna, vel scelerisque nisl consectetur et.`
)
.join("\n")}
</Box>
</Container>
<ScrollTop>
<Fab color="secondary" size="small" aria-label="scroll back to top">
<KeyboardArrowUpIcon />
</Fab>
</ScrollTop>
</React.Fragment>
);
}
ScrollTop
中使用的children
元素是ScrollTop
元素內 JSX 中的子元素。 在這種情況下是:
<Fab color="secondary" size="small" aria-label="scroll back to top">
<KeyboardArrowUpIcon />
</Fab>
這是將浮動操作按鈕傳遞給ScrollTop
,作為用戶滾動時顯示的內容,以及單擊時將導致頁面滾動回頂部的內容。
您問題中的代碼有ScrollTop(children)
而不是ScrollTop({children})
- 即您正在調用整個props
object children
,而不是讓children
擺脫它。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.