相关疑难解决方法(0)

React:在不使用this.props.children的情况下将组件作为prop传递

我有一个组件Foo.js:

// a svg component with a star.svg icon
import { IconStar } from 'react-svg-icons'; 

// call a button with a custom icon
<Button icon={ IconStar }> Click to trigger </Button>
Run Code Online (Sandbox Code Playgroud)

然后button.js我有:

render() {
  const theIcon = this.props.icon;
  return (
    <button>
      {this.props children}
      {icon() /*this works but i can't pass props inside it*/} 
      <theIcon className={ Styles.buttonIcon } />
    </button>
  )
}
Run Code Online (Sandbox Code Playgroud)

我想在<IconStar>里面渲染<Button>,我该怎么做?

我不能通过这个,this.prop.children因为它在图标道具周围有更多的逻辑,但在这种情况下我只显示一个演示.

谢谢

javascript jsx reactjs

2
推荐指数
1
解决办法
3741
查看次数

标签 统计

javascript ×1

jsx ×1

reactjs ×1