最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - ReactJSX - changing element type conditionally with minimal code duplication - Stack Overflow

programmeradmin1浏览0评论

Let's say I have something that is:

<div>
  {/* a lot of code */}
</div>

But if some condition is true, I want it to be:

<tr>
  {/* same code as before */}
</tr>

Is there any way to achieve this without a huge amount of code duplication from copy pasting?

Let's say I have something that is:

<div>
  {/* a lot of code */}
</div>

But if some condition is true, I want it to be:

<tr>
  {/* same code as before */}
</tr>

Is there any way to achieve this without a huge amount of code duplication from copy pasting?

Share Improve this question edited Jun 30, 2018 at 10:23 Tholle 113k22 gold badges208 silver badges196 bronze badges asked Jun 30, 2018 at 5:09 rococorococo 2,6572 gold badges25 silver badges45 bronze badges 3
  • Functions. Why dont you wrap the whole thing inside into a function? – Sulthan Commented Jun 30, 2018 at 5:16
  • Good call, that worked fine for me. – rococo Commented Jun 30, 2018 at 5:23
  • Related stackoverflow.com/questions/33710833/… – Sulthan Commented Jun 30, 2018 at 5:36
Add a comment  | 

5 Answers 5

Reset to default 8

You could render the content in a variable using React.Fragment and choose the enclosing element depending on the condition.

Example

class App extends Component {
  state = { condition: true };

  render() {
    const { condition } = this.state;
    const content = (
      <Fragment>
        <h1>Hello world</h1>
        <h2>This is a lot of text...</h2>
      </Fragment>
    );

    return condition ? <div> {content} </div> : <tr> {content} </tr>;
  }
}

Marked someone else as accepted answer, but the solution I ended up using was to call React.createElement(condition ? "div" : "tr", {attribute: stuff}, <div>Inner content</div>). Just an alternative for anyone who stumbles upon this in the future :)

Add your code inside the function

function returnView() {
  render (
  // a lot of code
);
}

if condition is true call that function

condition? returnView() : ' '

Have you tried with createElement() method? It works well for me. So the code is:

import { createElement } from 'react';

createElement(type, props, ...children)

Example:

function Element({ condition, children, onClick }) {

  if (condition) {
    return createElement(
      'div',
      { className: 'element-style' },
      children
    );
  }

  return createElement(
    'tr',
    { className: 'element-style' },
    children
  );
}

If you have some of the special props and the rest of them is same, you can pass it in object variable

function Element({ condition, children, onClick }) {

  const elementProps = {
    className: 'element-style',
    onClick,
  };

  if (condition) {
    return createElement(
      'div',
      { ...elementProps, style: { color: 'white' } },
      children
    );
  }

  return createElement(
    'tr',
    { ...elementProps, style: { color: 'black' } },
    children
  );
}

Source: https://beta.reactjs.org/reference/react/createElement

Create a component like this

const ContentContainer = ({as, children, ...props}) => {
    return React.createElement(as ?? 'div', ...props, ...children)
}

Usage

<ContentContainer as="tr" className="main-container">
     // a lot of code
</ContentContainer>
发布评论

评论列表(0)

  1. 暂无评论