配列を<li>
要素のコレクションとしてレンダリングしようとしています。私はReact with ES2015構文とBabelを翻訳に使用しています。Array.prototype.mapは未定義を返します
配列はオブジェクトに格納され、this.props.technologies
としてアクセスすることができますので、私はこの書き込み時に:私はしようとすると、
<ul class="categories" is="null">
coffeescript
atom
</ul>
しかし:
<ul className="categories">
{ this.props.technologies }
</ul>
を私は次のHTML出力を取得しますこれは:
<ul className="categories">
{
this.props.technologies.map(c => {
return `<li>${c}</li>`
})
}
私はというエラーが出ます:this.props.technologies is undefined
私もこれをやってみました:マップが動作している場合だけチェックする
this.props.technologies.map(c => c)
が、それはまだ同じエラーになります。
編集
私はそれをテストしてみました、と私はAJAXを使用してデータをフェッチする場合にのみそれが起こることがわかりました。
私はtest.jsonにMVCEに
import React, {Component} from 'react'
import axios from 'axios'
class Header extends Component {
constructor(props) {
super(props)
this.state = {}
}
render() {
return(
<ul>
{
this.props.technologies.map(c => <li>{c}</li>)
}
</ul>
)
}
}
class Details extends Component {
constructor(props) {
super(props)
this.state={}
}
componentWillMount(){
axios.get(`/static/dummydata/test.json`)
.then(res => this.setState(res.data))
.catch(res=>console.log(res))
}
render(){
return(
<Header technologies={this.state.technologies}/>
)
}
}
export {Header, Details}
データを提供しています2 編集は次のとおりです。
{
"technologies": [
"coffeescript",
"js"
]
}
あなたは 'this.props.technologies'があなたの例では配列でないかもしれない –
と書かれた総数を共有することができます – anvk
技術が実際に配列か配列のようなオブジェクトかどうかを確認することがあります。例えば、 'Array.isArray(this.props.technologies)'によってそれを行うことができます。 –