Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/408.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 无法在jsx的呈现中获取状态值 render(){ const{params}=this.props; const actionId=params.id; console.log(actionId)//具有值 返回( {this.actionId} ) }_Javascript_Reactjs - Fatal编程技术网

Javascript 无法在jsx的呈现中获取状态值 render(){ const{params}=this.props; const actionId=params.id; console.log(actionId)//具有值 返回( {this.actionId} ) }

Javascript 无法在jsx的呈现中获取状态值 render(){ const{params}=this.props; const actionId=params.id; console.log(actionId)//具有值 返回( {this.actionId} ) },javascript,reactjs,Javascript,Reactjs,我没有看到上面代码的任何输出,我想知道为什么。我可以在控制台中看到值。怎么了?此.actionId未在您的上下文中定义,此是指self中的组件 render() { const { params } = this.props; const actionId = params.id; console.log(actionId) // has value return( <div> {this.actionId}

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

此.actionId
未在您的上下文中定义,
是指self中的组件

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

    console.log(actionId) // has value

    return(
        <div>
           {this.actionId}
        </div>
    )
 }
render(){
const{params}=this.props;
const actionId=params.id;
console.log(actionId)//具有值
返回(
{actionId}
)
}

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

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

    console.log(actionId) // has value

    return(
        <div>
           {this.actionId}
        </div>
    )
 }
render(){
const{params}=this.props;
const actionId=params.id;
console.log(actionId)//具有值
返回(
{actionId}
)
}

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

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

    console.log(actionId) // has value

    return(
        <div>
           {actionId}
        </div>
    )
 }
render(){
const{params}=this.props;
const actionId=params.id;
console.log(actionId)//具有值
返回(
{actionId}
)
}

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

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

    console.log(actionId) // has value

    return(
        <div>
           {actionId}
        </div>
    )
 }
render(){
const{params}=this.props;
const actionId=params.id;
console.log(actionId)//具有值
返回(
{actionId}
)
}

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

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

    console.log(actionId) // has value

    return(
        <div>
           {actionId}
        </div>
    )
 }
render(){
console.log(this.props.id)//具有值
返回(
{this.props.id}
)
}

我认为如果您将来共享您的项目,使用
params
作为
this.props
的别名会让其他开发人员感到困惑。你可以毫无意义地把代码复杂化。

是的,我同意上面的答案。 但你为什么不这么做呢

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

    console.log(actionId) // has value

    return(
        <div>
           {actionId}
        </div>
    )
 }
render(){
console.log(this.props.id)//具有值
返回(
{this.props.id}
)
}

我认为如果您将来共享您的项目,使用
params
作为
this.props
的别名会让其他开发人员感到困惑。你会无缘无故地使你的代码复杂化。

原因是假设他在道具中传递5个值,然后每次他都要写
这个.props.
,但使用
{params}=this.props
,他可以很容易地避免,这会使代码更可读,而且更小。原因是假设他在道具中传递5个值,然后,每次他必须编写
this.props.
,但是使用
{params}=this.props
,他可以很容易地避免这种情况,这使得代码更具可读性,而且更小。