首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >我的react组件返回语句无法呈现,但是console.log正好显示了我所需要的

我的react组件返回语句无法呈现,但是console.log正好显示了我所需要的
EN

Stack Overflow用户
提问于 2022-01-27 15:25:31
回答 1查看 83关注 0票数 -2

我是新的反应和工作的反应视频播放器。我在执行评论部分有问题。

这是我的视频播放器组件本身。

代码语言:javascript
运行
复制
    export default function VidPlayer() {

  // useStates
  const [state, setState] = useState({
    playing: true,
  });
  const [comments, setComments] = useState([]);

  const [comment, setComment] = useState("");

  const { playing } = state;

  const playerRef = useRef(null);

  // declaring functions for video player buttons
  const handlePlayPause = () => {
    setState({ ...state, playing: !state.playing });
  };
  const handleRewind = () => {
    playerRef.current.seekTo(playerRef.current.getCurrentTime() - 5);
  };
  const handleFoward = () => {
    playerRef.current.seekTo(playerRef.current.getCurrentTime() + 5);
  };
  const handleStop = () => {
    playerRef.current.seekTo(0);
    setState({ playing: !state.playing });
  };

  // declaring functions for comment section
  const addComments = () => {
    if (comment) {
      setComments({...comments, comment});
      setComment("");
      console.log("Hello", comments);
    }
  };

  const handleComment = (e) => {
    setComment(e.target.value);
  };


  return (
    <div>
      <AppBar style={{ background: "#e6880e" }} position="static">
        <Toolbar>
          <Typography variant="h6" component="div" sx={{ flexGrow: 1 }}>
            Favour's Video Player
          </Typography>
        </Toolbar>
      </AppBar>

      {/* container for the videoplayer, buttons and comment section */}
      <div className="container">
        <>
          {/* videoplayer */}
          <div className="reactPlayer one">
            <ReactPlayer
              ref={playerRef}
              url="https://www.youtube.com/watch?v=1_ATK0BLc8U&t=3s"
              playing={playing}
              controls
            />
          </div>

          {/* buttons */}
          <div className="btn-stack two">
            <Stack spacing={2} direction="row">
              <Button
                style={{ background: "#e6880e" }}
                size="small"
                variant="contained"
                onClick={handlePlayPause}
              >
                Play
              </Button>
              <Button
                style={{ background: "#e6880e" }}
                size="small"
                variant="contained"
                onClick={handleRewind}
              >
                Rewind{" "}
              </Button>
              <Button
                style={{ background: "#e6880e" }}
                size="small"
                variant="contained"
                onClick={handleFoward}
              >
                Forward{" "}
              </Button>
              <Button
                style={{ background: "#e6880e" }}
                size="small"
                variant="contained"
                onClick={handleStop}
              >
                Stop
              </Button>
            </Stack>
          </div>

          {/* comment section */}
          <div className="comment three">
            <Comment userComs={comments} />
            <TextField
              placeholder="add comment"
              size="small"
              variant="outlined"
              onChange={handleComment}
              value={comment}
            />
            <Button
              style={{ background: "#e6880e", marginLeft: '1em' }}
              onClick={addComments}
              variant="contained"
              size="small"
            >
              Send
            </Button>
          </div>
        </>
      </div>
    </div>
  );
}

它接受了这个注释组件的结尾。

代码语言:javascript
运行
复制
export default function commentList(props) {
  console.log("Hello brian", props.userComs);

  const { userComs } = props;

  if (Object.keys(userComs).length > 0) {
    console.log(userComs);

    // userComs.forEach((element) => {
    // console.log("Im in", userComs);
      Object.values(userComs).forEach(val  => {
      // console.log("Im in", userComs);
      // console.log(val);

      return (
        <div>
        <List
          sx={{ width: "100%", maxWidth: 360, bgcolor: "background.paper" }}
        >
          
          <ListItem alignItems="flex-start">
            <ListItemAvatar>
              <Avatar alt="Remy Sharp" src="/static/images/avatar/1.jpg" />
            </ListItemAvatar>
            <ListItemText
              secondary={
                <React.Fragment>
                  <Typography
                    sx={{ display: "inline" }}
                    component="span"
                    variant="body2"
                    color="text.primary"
                  >
                    Ali Connors
                  </Typography>
                  {val}
                </React.Fragment>
                
              }
              
            />
          </ListItem>
          <Divider variant="inset" component="li" />
        </List>
        </div>
      );
      
    });
  } else {
    return <div></div>;
  }

}

这是前端在这里输入图像描述,不幸的是,当我输入注释并单击send时,屏幕变为空白,控制台抛出一个"nothing was returned from render"错误。有人能帮我检查一下哪里出了问题吗?我怎样才能解决这个问题?

EN

回答 1

Stack Overflow用户

发布于 2022-01-27 15:30:39

正如错误所述,组件没有返回任何内容。

代码语言:javascript
运行
复制
Object.values(userComs).forEach(val  => {

应该是

代码语言:javascript
运行
复制
return Object.values(userComs).map(val  => {

因为forEach不返回任何东西,而且在每次迭代中返回的JSX不会在任何地方使用,但是map返回一个新的数组,这个数组可以使用。

顺便说一句,一定要给每个从回调返回的div提供一个div支柱。

代码语言:javascript
运行
复制
<div key={val}> // assuming `val` is unique
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70881182

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档