未能在jsx的渲染中获取状态值

时间:2017-02-10 10:22:03

标签: javascript reactjs

render() {
    const { params } = this.props;
    const actionId = params.id;

    console.log(actionId) // has value

    return(
        <div>
           {this.actionId}
        </div>
    )
 }

我没有看到上面代码的任何输出,我想知道为什么。我可以在控制台中看到该值。怎么了?

3 个答案:

答案 0 :(得分:2)

您已将params.id存储在actionId变量中,因此请直接使用,删除this关键字,如下所示:

render() {
    const { params } = this.props;
    const actionId = params.id;

    console.log(actionId) // has value

    return(
        <div>
           {actionId}
        </div>
    )
 }

答案 1 :(得分:1)

this.actionId未在您的上下文中定义,this是指自我中的组件

render() {
    const { params } = this.props;
    const actionId = params.id;

    console.log(actionId) // has value

    return(
        <div>
           {actionId}
        </div>
    )
 }

答案 2 :(得分:1)

是的,我同意上述答案。 但为什么你不这样做呢? :

render() {

 console.log(this.props.id) // has value

 return(
     <div>
        {this.props.id}
     </div>
  )
}

如果您将来共享项目,我认为在params上使用this.props作为别名会让其他开发人员感到困惑。而且你的代码一无所获。