[英]I can't add elements from function?
Problem: I am working on a project. 问题:我正在开展一个项目。 Values come from another component. 值来自另一个组件。 According to these values, I want to add elements in the "render". 根据这些值,我想在“渲染”中添加元素。 But I couldn't do it. 但我不能这样做。 Here is the function: 这是功能:
isVisible = () => {
this.props.contents.contents.map( value => {
console.log(value)
console.log(Object.values(value)[0])
if(Object.keys(value)[0] === "mushroom" ){
if(Object.values(value)[0] === true){
alert("true")
return(<div style={{
position: "absolute",
top: "0",
left: "0",
width: "100px",
height: "100px",
background: 'red',
opacity: "1",
zIndex: "100"
}}>...</div>)
}
}
})
}
There's no problem with the Props. 道具没有问题。 It turns true. 它变成了现实。 So the "alert" works. 所以“警报”有效。 But I couldn't print the in return here: 但我无法在这里打印回报:
render(){
return(
<Wrap>
<div className={classes.imageBox}>
<div className={classes.image}>
{this.isVisible()}
</div>
</div>
</Wrap>
)
}
How do I solve this problem? 我该如何解决这个问题?
It is not returning anything 它没有返回任何东西
isVisible = () => {
this.props.contents.contents.map(...) . <-- this is not returned
}
so it returns undefined when you call isVisible. 所以当你调用isVisible时它会返回undefined。 You need to add return statement or drop the brackets. 您需要添加return语句或删除括号。
so 所以
isVisible = () => {
return this.props.contents.contents.map(...)
}
or 要么
isVisible = () =>
this.props.contents.contents.map(...)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.