简体   繁体   English

在功能组件中返回 React 元素

[英]Returning a React Element within a Functional Component

I am fairly new to React and trying to edit a former coworker's code (hence the non-traditional React style).我对 React 相当陌生,并试图编辑前同事的代码(因此是非传统的 React 风格)。

I need to return a series of buttons (here "Hello" and "Goodbye") when a single button ("Dropdown") is clicked.单击单个按钮(“下拉菜单”)时,我需要返回一系列按钮(此处为“Hello”和“Goodbye”)。 In order to make this more dynamic down the line, I'd like to return those buttons from a function (dropdownDetails) rather than [show,setShow] variables, etc.为了使这更加动态,我想从 function (dropdownDetails)而不是 [show,setShow] 变量等返回这些按钮。

When I run this code, the console shows that dropdownDetails is triggered, but "Hello" and "Goodbye" don't appear in the UI.当我运行此代码时,控制台显示触发了 dropdownDetails,但 UI 中没有出现“Hello”和“Goodbye”。 What am I doing wrong?我究竟做错了什么?

Thanks in advance!提前致谢!

///// In base.js:
const e = React.createElement;

///// in page_body.js:
function pageBody(props) {

    const dropdownDetailsWrap = (props) => (e) => {
      console.log("Wrap triggered")
      dropdownDetails()
    }
    
    function dropdownDetails() {
        console.log("dropdownDetails useEffect triggered")
        return( 
            e("button",{type:"button"},"Hello"),
            e("button",{type:"button"},"Goodbye")
             );
    };
    
    const pageBody = () => (e) => {
    return(
        e("button", {type:"button",onClick:dropdownDetailsWrap(),className:'btn'},"Dropdown")
        )}
    }

ReactDOM.render(e(pageBody), document.querySelector('#page_body'));

Note: I know there are more elegant ways to make a dropdown, particularly using packages.注意:我知道有更优雅的方法来制作下拉菜单,尤其是使用包。 Just trying to understand the basics with pure React for now.现在只是试图理解纯 React 的基础知识。

Probably you can do like this provided you use the useState hook of react.如果您使用 react 的 useState 钩子,您可能可以这样做。

Wihtout using state, I doubt we can do this.如果不使用 state,我怀疑我们能做到这一点。

I am pasting the code below, check it out on codesandbox to explore more.我正在粘贴下面的代码,请在代码沙箱上查看以了解更多信息。

Here is the link Link这是链接 链接

Code:代码:

import React, { useState } from "react";
const e = React.createElement;

export default function App() {
  return (
    <div className="App">
      <PageBody />
    </div>
  );
}

function PageBody(props) {
  var buttonsToShow;
  const [showButtons, setshowButtons] = useState(false);
  const [buttons, setButtons] = useState([]);

  const dropdownDetailsWrap = (props) => {
    console.log("Wrap triggered");
    buttonsToShow = dropdownDetails();
    setButtons(buttonsToShow);
    setshowButtons(true);
  };

  function dropdownDetails() {
    console.log("dropdownDetails useEffect triggered");
    return [
      e("button", { type: "button", key: 1 }, "Hello"),
      e("button", { type: "button", key: 2 }, "Goodbye")
    ];
  }

  var dropbutton = () => {
    return e(
      "button",
      { type: "button", onClick: dropdownDetailsWrap, className: "btn" },
      "Dropdown"
    );
  };

  return (
    <div>
      {dropbutton()} <br /> {showButtons && buttons}
    </div>
  );
}

Now,I know this is not the best version but still you can clean this code and use the logic.现在,我知道这不是最好的版本,但您仍然可以清理此代码并使用逻辑。 Also by using state your code does not become static, moreover you get to customize more things,此外,通过使用 state,您的代码不会变成 static,而且您可以自定义更多的东西,

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM