【发布时间】:2019-03-01 11:30:21
【问题描述】:
我是 react.js 新手,在执行以下任务时遇到问题。
按照代码运行时,显示setState 无法使用,应用中断。我有这两个对象,obj2 生成一个表3 columns。我需要使definition 列的每个单元格都不是null 可点击的,然后将我带到team_wiki 链接,该链接存在于obj1 中。
obj1: {
providers : [
{team_name : "XYZ", team_wiki : "https://example.com", team_id : 1},
{team_name : "ABC", team_wiki : "null", team_id : 2},
{team_name : "LMN", team_wiki : "https://example2.com", team_id : 3},
{team_name : "MNO", team_wiki : "https://example3.com", team_id : 4}
]
}
obj2: {
products : [
{team_name : "XYZ", definition : "this team handles XYZ products", metric_id : 101},
{team_name : "ABC", definition : "this team handles ABC products", metric_id : 201},
{team_name : "LMN", definition : "this team handles LMN products", metric_id : 301},
{team_name : "MNO", definition : "this team handles MNO products", metric_id : 401}
]
}
代码:
state = {
API_DATA : {},
TEAM_WIKI : ''
}
componentDidMount(){
// fetch the obj1 data and sets it to the state called API_DATA
}
wikiLink = (TEAM_NAME) {
// getting TEAM_NAME from a component inside the render method
const wiki = this.state.API_DATA.map(provider => {
if (provider.team_name = TEAM_NAME && provider.team_wiki !== null) {
return provider.team_wiki
}
})
.map(link => {
if (link !== undefined) {
return link
}
})
this.setState({
// TEAM_WIKI is a state { its a string } where i want to store the
//team_wiki at last
TEAM_WIKI : wiki
})
}
render() {
return (
// i want to use it something like this
< href="this.state.TEAM_WIKI" onClick={this.wikiLink(TEAM_NAME)} />
)
}
【问题讨论】:
-
您从控制台返回的错误是什么?
标签: javascript reactjs ecmascript-6 lodash ecmascript-2017