gpt4 book ai didi

javascript - React - 用户定义的 JSX 组件不呈现

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:41:27 26 4
gpt4 key购买 nike

我一直在尝试进行 AJAX 调用,然后在检索到它后将其添加到我的 View 中。

当前代码实际上没有发生任何事情。

const View = () => (
<div>
<h1>Reports</h1>
<statisticsPage />
</div>
);
export default View;


var statisticsPage = React.createClass({
getInitialState: function() {
return {info: "loading ... "};
},
componentDidMount: function() {
this.requestStatistics(1);
},
render: function() {
return (
<div>info: {this.state.info}</div>
);
},
requestStatistics:function(){
axios.get('api/2/statistics')
.then(res => {
values = res['data']
this.setState({info:1})
console.log('works!!')
});

}

})

最佳答案

您的组件名称必须以大写字符开头,因为以小写字母开头的组件名称将作为默认 DOM 元素进行搜索,例如 div, p, span etc .您的 statisticsPage 不是这种情况成分。将其设为大写即可。

根据docs :

When an element type starts with a lowercase letter, it refers to a built-in component like or and results in a string 'div' or 'span' passed to React.createElement. Types that start with a capital letter like <Foo /> compile to React.createElement(Foo) and correspond to a component defined or imported in your JavaScript file.

We recommend naming components with a capital letter. If you do have a component that starts with a lowercase letter, assign it to a capitalized variable before using it in JSX.

const View = () => (
<div>
<h1>Reports</h1>
<StatisticsPage />
</div>
);



var StatisticsPage = React.createClass({
getInitialState: function() {
return {info: "loading ... "};
},
componentDidMount: function() {
this.requestStatistics();
},
render: function() {
return (
<div>info: {this.state.info}</div>
);
},
requestStatistics:function(){
console.log('here');
this.setState({info:1})
console.log('works!!')


}

})

ReactDOM.render(<View/>, document.getElementById('app'));
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>

<div id="app"></div>

关于javascript - React - 用户定义的 JSX 组件不呈现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41216654/

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