相关疑难解决方法(0)

我如何有条件地包装React组件?

我有一个组件,有时需要作为一个锚呈现,其他时候作为一个简单的div.在<anchor>我触发关断的,以确定所需要的是<div>道具.如果它存在,我需要渲染包含在锚点中的组件prop.否则它只是呈现为this.props.url.

可能?

这就是我现在正在做的事情,但感觉它可以简化:

if (this.props.link) {
    return (
        <a href={this.props.link}>
            <i>
                {this.props.count}
            </i>
        </a>
    );
}

return (
    <i className={styles.Icon}>
        {this.props.count}
    </i>
);
Run Code Online (Sandbox Code Playgroud)

更新:

这是最后的锁定.谢谢你的提示,@ Sulthan!

import React, { Component, PropTypes } from 'react';
import classNames from 'classnames';

export default class CommentCount extends Component {

    static propTypes = {
        count: PropTypes.number.isRequired,
        link: PropTypes.string,
        className: PropTypes.string
    }

    render() {
        const styles = require('./CommentCount.css');
        const {link, className, count} = this.props;

        const iconClasses …
Run Code Online (Sandbox Code Playgroud)

javascript reactjs

66
推荐指数
5
解决办法
2万
查看次数

标签 统计

javascript ×1

reactjs ×1