这是一个小代码段。我试图使用this.state,但它不起作用。
import React from "react";
import Header from './Header';
import Footer from './Footer';
export default class Layout extends React.Component{
constructor(){
super();
this.state = {name: "sanu"};
}
render(){
return(
<div>
{this.state.name}
<Header />
<Footer />
</div>
);
}
}
答案 0 :(得分:0)
我把你的代码放在一个codepen中
https://codepen.io/anon/pen/JJXjPe#anon-login
工作正常。你在经营巴贝尔吗?
HTML
<div id="app"></div>
JS
class Layout extends React.Component{
constructor(){
super();
this.state = {name: "sanu"};
}
render(){
return(
<div>
{this.state.name}<br/>
Header <br/>
Footer <br/>
</div>
);
}
}
ReactDOM.render(
<Layout/>,
document.getElementById('app')
);