简体   繁体   English

在React构造函数中未调用Console.Log

[英]Console.Log Not Being Called Inside React Constructor

I'm trying to add a component to a default .NET Core MVC with React project. 我正在尝试将组件添加到带有React项目的默认.NET Core MVC中。 I believe I have everything wired up to mirror the existing "Fetch Data" component, but it doesn't seem like it's actually being called (but the link to the component in my navbar does move to a new page). 我相信我已经将所有东西都连接起来以镜像现有的“获取数据”组件,但是似乎并没有真正调用它(但是导航栏中指向该组件的链接确实移到了新页面)。

The component itself... 组件本身...

import React, { Component } from 'react';

export class TestComponent extends Component {
    static displayName = TestComponent.name;

    constructor (props) {
        super(props);
        console.log("WHO NOW?");
        this.state = { message: '', loading: true, promise: null };

        this.state.promise = fetch('api/SampleData/ManyHotDogs');

        console.log(this.state.promise);
    }

    static renderForecastsTable (message) {
        return (
            <h1>
                Current Message: {message}
            </h1>
        );
    }

    render () {
        let contents = this.state.loading

            ? <p><em>Loading...</em></p>
            : TestComponent.renderForecastsTable(this.state.message);

        return (

            <div>
                <h1>Weather forecast</h1>
                <p>This component demonstrates fetching data from the server.</p>
                {contents}
            </div>
        );
    }
}

The App.js App.js

import React, { Component } from 'react';
import { Route } from 'react-router';
import { Layout } from './components/Layout';
import { Home } from './components/Home';
import { FetchData } from './components/FetchData';
import { Counter } from './components/Counter';
import { TestComponent } from './components/TestComponent';

export default class App extends Component {
  static displayName = App.name;

  render () {
    return (
      <Layout>
        <Route exact path='/' component={Home} />
        <Route path='/counter' component={Counter} />
        <Route path='/fetch-data' component={FetchData} />
        <Route path='/test-controller' component={TestComponent} />
      </Layout>
    );
  }
}

That console.log("Who now") is never called when I inspect, and the page remains totally blank. 我检查时从未调用该console.log(“ Who now”),并且该页面完全空白。 I can't find a key difference between this and the functioning components, and google has not been much help either. 我找不到这和功能组件之间的关键区别,谷歌也没有太大的帮助。 Any ideas what is missing? 任何想法缺少什么?

Edit 编辑

While troubleshooting this, I ended up creating a dependency nightmare that broke the app. 在对此进行故障排除时,我最终造成了导致应用程序崩溃的依赖噩梦。 Since I'm only using the app to explore React, I nuked it and started over--and on the second attempt I have not been able to reproduce the not-rendering issue. 由于我仅使用该应用程序来浏览React,因此我将其删除并重新开始-在第二次尝试中,我无法重现未渲染的问题。

It is advisable to use componentDidMount to make the call to the REST API with the fetch or axios. 建议使用componentDidMount通过fetch或axios调用REST API。


class TestComponent extends Component{
  constructor(props){
    state = {promise: ''}
  }
  async componentDidMount () {
    let promise = await fetch ('api / SampleData / ManyHotDogs');
    this.setState ({promise});
    console.log (promise);
  }
  render(){
    return(
        <div>{this.state.promise}</div>
    );
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM