gpt4 book ai didi

javascript - 如何从类json数组reactjs返回

转载 作者:行者123 更新时间:2023-12-02 23:40:25 28 4
gpt4 key购买 nike

嗨,我有这段代码,可以从文件 config.json 获取并返回 json 数据

文本.js

 class Text extends React.Component {
constructor(props){
super(props)
this.state = {
datat: [],
};
}
componentDidMount(){
fetch('/config.json')
.then(response => response.json())
.then((datao) =>{
this.setState({
datat: (JSON.parse(JSON.stringify(datao)))
})

});
}
render(){
const datatorender = this.state.datat;
return ( Object.keys(datatorender).map(key =>{if(key==this.props.value){return datatorender[this.props.value]}}))
}}

我在家里的称呼是这样的:home.js

<Text value="SITENAME">

所以我想这样调用它:{text.SITENAME} 而不是第一个我该怎么做?

这是 json 文件:

{
"SITENAME": "site name",
"SITE_DESCRIPTION":"desc"
}

最佳答案

试试这个:

class Text extends React.Component {
constructor(props){
super(props)
this.props = props;
this.state = {
datat: [],
};
}
componentDidMount(){
fetch('/config.json')
.then(response => response.json())
.then((datao) =>{
this.setState({
datat: (JSON.parse(JSON.stringify(datao)))
})

});
}
render() {
const datatorender = this.state.datat;
console.log(datatorender)
return (
<div>
{
Object.keys(datatorender).map((key, i) => {
if (key === this.props.value) {
return (
<li key={i}> {datatorender[this.props.value]} </li>
)
} else {
return null
}
})
}
</div>
)
}
}

关于javascript - 如何从类json数组reactjs返回,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56105007/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com