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
,他可以很容易地避免这种情况,这使得代码更具可读性,而且更小。