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

javascript - ReactDOM.render Expected the last optional `callback` argument to be a function - Stack Overflow

programmeradmin2浏览0评论

I am new to react and I wrote the code below and got

ReactDOM.render Expected the last optional `callback` argument to be a function. `Instead received: Object` 

This is my code

var Stats = React.createClass({
    render: function () {
        return (
            <article className="col-md-4">
                <article className="well">
                    <h3>{this.props.value}</h3>
                    <p>{this.props.label}</p>
                </article>
            </article>
        )
    }
});

ReactDOM.render(
    <Stats value={"255.5K"} label={"People engaged"}/>,
    <Stats value={"5K"} label={"Alerts"}/>,
    <Stats value={"205K"} label={"Investment"}/>,
    document.getElementById('stats')
);

What am I doing wrong?

I am new to react and I wrote the code below and got

ReactDOM.render Expected the last optional `callback` argument to be a function. `Instead received: Object` 

This is my code

var Stats = React.createClass({
    render: function () {
        return (
            <article className="col-md-4">
                <article className="well">
                    <h3>{this.props.value}</h3>
                    <p>{this.props.label}</p>
                </article>
            </article>
        )
    }
});

ReactDOM.render(
    <Stats value={"255.5K"} label={"People engaged"}/>,
    <Stats value={"5K"} label={"Alerts"}/>,
    <Stats value={"205K"} label={"Investment"}/>,
    document.getElementById('stats')
);

What am I doing wrong?

Share Improve this question asked Sep 20, 2016 at 21:57 elad silverelad silver 9,7056 gold badges46 silver badges70 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 6

You are giving ReactDom.render four arguments - three Stats ponents and the element. The function expects only one element before the container element. Thus you need to somehow group the elements together, for example like this:

ReactDOM.render(
  <div>
    <Stats value={"255.5K"} label={"People engaged"}/>
    <Stats value={"5K"} label={"Alerts"}/>
    <Stats value={"205K"} label={"Investment"}/>
  </div>,
  document.getElementById('stats')
);

You need provide a react element as first argument and mount node as second argument exactly. So you can do this.

const LotsOfStats = React.createClass({
    render: function () {
        return (
            <div>
                <Stats value={"255.5K"} label={"People engaged"}/>
                <Stats value={"5K"} label={"Alerts"}/>
                <Stats value={"205K"} label={"Investment"}/>
            </div>
        );
    }
});

ReactDOM.render(
    <LotsOfStats />,
    document.getElementById('stats')
);
发布评论

评论列表(0)

  1. 暂无评论