React包括组件名称中的动态prop值

时间:2018-02-28 04:46:46

标签: javascript reactjs components

我正在尝试渲染反应组件。组件的第一部分将保持不变,但第二部分需要根据从props返回的数据动态更改。有没有办法将这些连接在一起?

import * as Icon from 'react-cryptocoins';

class ResultRow extends PureComponent {
  render() {
    return (
      <div className="component-result-row">

        <Icon.{this.props.name}/>
      </div>
    );}}

硬编码名称按预期工作

<{Icon.image1}/>

解决方案:

import * as Icon from 'react-cryptocoins';

class ResultRow extends PureComponent {
  var name = this.props.name;
  var Component = Icon[symbol];
  render() {
    return (
      <div className="component-result-row">

       <Component/>
  </div>
);}}

1 个答案:

答案 0 :(得分:3)

一种可能的方法是将值存储在变量中(应以大写字母开头)然后渲染它。

像这样:

mDrawerLayoutLeft.addDrawerListener(new DrawerLayout.DrawerListener() {

        @Override
        public void onDrawerOpened(View drawerView) {
            //lock the right side drawer when open the left drawer
            mDrawerLayoutRight.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);
        }

        @Override
        public void onDrawerClosed(View drawerView) {
             //unlock the right side drawer when close the left  drawer
            mDrawerLayoutRight.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED);
        }

    });