简体   繁体   English

React 组件 - 有更好的方法吗?

[英]React Component - is there a better way?

My first React component.我的第一个 React 组件。 Kind of pointless but it's a starting point.有点无意义,但这是一个起点。 I have a page and want to render all the headers on the page.我有一个页面,想要呈现页面上的所有标题。 I have many similar elements thus (with different values for data-headertext):因此,我有许多类似的元素(data-headertext 具有不同的值):

<div class="col-md-12 col-sm-12 p-0 headerText" data-headertext="Browse"></div>

I have built a React class component thus:因此,我构建了一个 React class 组件:

class Header extends React.Component {
  constructor(props) {
    super(props);
  }

  render() {
    return (
      <React.Fragment>
        <div className="row">
          <div className="col-md-6 col-sm-6"><h5 className="pl-2">{this.props.headertext}</h5></div>
          <div className="col-md-6 col-sm-6 visHiddenRight" id={this.props.headertext + "BackDiv"}><h5 className="pr-2 mouseover" onClick={() => {back(this.props.headertext);}}>Back</h5></div>
        </div>
        <hr className="hrOrange mt-2" />
      </React.Fragment>
    );
  }
}

let elems = document.getElementsByClassName("headerText");

ReactDOM.render(
  renderToElements(Header, elems, 'headertext'), document.getElementById("root")
);

function renderToElements(toRender, elements, dataset) {
  for (var i = 0; i < elements.length; i++) {
    let passText = elements[i].dataset[dataset];
    let renderEl = React.createElement(toRender, { headertext: passText })
    ReactDOM.render(renderEl, elements[i]);
  }
}

It works perefectly.它完美地工作。 EXCEPT.除了。

I've had to add a dummy div tag which is not displayed to fulfil the ReactDOM.render parameters, thus:我必须添加一个虚拟 div 标签,该标签不会显示以满足 ReactDOM.render 参数,因此:

<div id="root" style="display:none;"></div>

This doesn't seem right.这似乎不对。 How should I be handling this?我应该如何处理这个? So I don't need to have this hidden div tag.所以我不需要这个隐藏的 div 标签。 Thanks in advance.提前致谢。

I would do something along the lines of:我会按照以下方式做一些事情:

// Header.ts
<div>
   { props ? <Header {...data} /> : null }
</div>

I wouldn't rely on CSS to hide content like this.我不会依赖 CSS 来隐藏这样的内容。 It defeats the point of using React.它破坏了使用 React 的意义。 Another idea would be to show a spinner while you're waiting on your params to load.另一个想法是在您等待参数加载时显示一个微调器。

Unrelated and I could be wrong as I use functional React instead of class based React but you don't need to use React.Fragment anymore.无关,我可能是错的,因为我使用功能性 React 而不是基于 class 的 React,但您不再需要使用 React.Fragment。 Just use <></>.只需使用<>/>。

暂无
暂无

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

相关问题 是否有更好的方法将prop传递给React中的组件? - Is there a better way to pass props to a component in React? 用 Hooks 编写这个 React Class 组件的更好方法是什么? - A better way to write this React Class Component with Hooks? 有没有更好的方法在 React Component 类中绑定“this”? - Is there a better way to bind 'this' in React Component classes? 在 React 中将 props 传递给组件的更好方法是什么? - What is the better way for passing props to component in React? 为什么这个反应组件需要很长时间才能重新渲染,有没有更好的方法来编写它? - Why is this react component taking ages to re-render, is there a better way of writing this? 有没有更好的方法来更新数组中代表 React 组件状态的某个对象? - Is there a better way to update some object in array which represents state for React component? 除了这个,有没有更好的方法从 React 的父组件调用子函数? - Is there a better way to call a child function from a parent component in React, other than this? 有没有更好、更简洁的方法来编写这个 Yup 模式验证器(由 React Formik 组件使用)? - Is there a better, cleaner way to write this Yup schema validator (used by React Formik component)? 对自定义事件做出反应的更好方法 - A better way to react to custom events 对于React.js的Component,哪种语法更好 - Which grammar is better for Component of React.js
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM