最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - React context - 'contextType' is not defined - Stack Overflow

programmeradmin3浏览0评论

I am using [email protected] and [email protected] that should support react Context and trying to run a simple example same as the react-context:

app.js

import React, { Component } from 'react';
import AppManger from './components/AppManger';
import './App.css';
export const ThemeContext = React.createContext({a1:'a1'});

class App extends Component {

  render() {

    return (

      <div className="App">
        <h1>Manage Storefront Services Products</h1>
        <ThemeContext.Provider value="dark">
          <AppManger />
        </ThemeContext.Provider>

      </div>

    );
  }
}

export default App;

AppManger.js(has no context reference)

import React, { Component } from 'react'
import SearchBar from './SearchBar';
export default class AppManger extends Component {


    constructor(props) {
        super(props);
        this.onSearchBarChange = this.onSearchBarChange.bind(this);
        this.state = {
            searchValue: '',
            errorLoading: false,
            errorObj: null,
        }
    }

    onSearchBarChange(e) {
        e.persist();
        this.setState({ searchValue: e.target.value });
    }

    render() {
        return (

            <div>
                <a href="/subsadmin/saml/logout">Log out</a>
                <SearchBar onSearchBarChange={this.onSearchBarChange} inAttrView={this.state.onAttrPage} />
            </div>

        )
    }
}

And the SearchBar.js where I want to use Context:

import React, { Component } from 'react';
import ThemeContext from '../App';


export default class SearchBar extends Component {
  constructor(props) {
    super(props);
    this.state = {
      showModal: false,
      showAttrModal: false
    };

  };


  componentDidMount(){
    console.log(this.context); //{}
  }

  render() {
    const contextType = ThemeContext;
    console.log(contextType); //{}
    return (

      <div>
        {contextType} /*'contextType' is not defined  no-undef */
        <input type="text" style={searchBoxStyle} className="form-control" onChange={this.props.onSearchBarChange} placeholder="Search for..." id="sku" name="sku" />

      </div>

    )
  }
}

If I run the app I get Line 44: 'contextType' is not defined no-undef in SearchBar.js if I remove this line I get {} when I logging the this.context.

I am using [email protected] and [email protected] that should support react Context and trying to run a simple example same as the react-context:

app.js

import React, { Component } from 'react';
import AppManger from './components/AppManger';
import './App.css';
export const ThemeContext = React.createContext({a1:'a1'});

class App extends Component {

  render() {

    return (

      <div className="App">
        <h1>Manage Storefront Services Products</h1>
        <ThemeContext.Provider value="dark">
          <AppManger />
        </ThemeContext.Provider>

      </div>

    );
  }
}

export default App;

AppManger.js(has no context reference)

import React, { Component } from 'react'
import SearchBar from './SearchBar';
export default class AppManger extends Component {


    constructor(props) {
        super(props);
        this.onSearchBarChange = this.onSearchBarChange.bind(this);
        this.state = {
            searchValue: '',
            errorLoading: false,
            errorObj: null,
        }
    }

    onSearchBarChange(e) {
        e.persist();
        this.setState({ searchValue: e.target.value });
    }

    render() {
        return (

            <div>
                <a href="/subsadmin/saml/logout">Log out</a>
                <SearchBar onSearchBarChange={this.onSearchBarChange} inAttrView={this.state.onAttrPage} />
            </div>

        )
    }
}

And the SearchBar.js where I want to use Context:

import React, { Component } from 'react';
import ThemeContext from '../App';


export default class SearchBar extends Component {
  constructor(props) {
    super(props);
    this.state = {
      showModal: false,
      showAttrModal: false
    };

  };


  componentDidMount(){
    console.log(this.context); //{}
  }

  render() {
    const contextType = ThemeContext;
    console.log(contextType); //{}
    return (

      <div>
        {contextType} /*'contextType' is not defined  no-undef */
        <input type="text" style={searchBoxStyle} className="form-control" onChange={this.props.onSearchBarChange} placeholder="Search for..." id="sku" name="sku" />

      </div>

    )
  }
}

If I run the app I get Line 44: 'contextType' is not defined no-undef in SearchBar.js if I remove this line I get {} when I logging the this.context.

Share Improve this question edited Dec 25, 2018 at 17:06 Itsik Mauyhas asked Dec 25, 2018 at 16:56 Itsik MauyhasItsik Mauyhas 3,98415 gold badges74 silver badges119 bronze badges 9
  • I guess it's should be const contextType = ThemeContext – Quynh Nguyen Commented Dec 25, 2018 at 16:58
  • Yes, but it still shows {} instead of real value. – Itsik Mauyhas Commented Dec 25, 2018 at 17:04
  • Your ThemeContext is a component or a value? – Quynh Nguyen Commented Dec 25, 2018 at 17:08
  • a value defined like -export const ThemeContext = React.createContext({a1:'a1'}); in App.js – Itsik Mauyhas Commented Dec 25, 2018 at 17:09
  • 1 Please try this import { ThemeContext } from '../App' – Quynh Nguyen Commented Dec 25, 2018 at 17:10
 |  Show 4 more comments

3 Answers 3

Reset to default 13

You aren't correctly using context, as you need to define it as a static property of the class and import it as a named import since you have exported it as a named import

import { ThemeContext } from '../App';

export default class SearchBar extends Component {
  constructor(props) {
    super(props);
    this.state = {
      showModal: false,
      showAttrModal: false
    };

  };

  static contextType = ThemeContext;
  componentDidMount(){
    console.log(this.context); //{}
  }

  render() {

    console.log(this.contextType); //{}
    return (

      <div>
        {contextType} /*'contextType' is not defined  no-undef */
        <input type="text" style={searchBoxStyle} className="form-control" onChange={this.props.onSearchBarChange} placeholder="Search for..." id="sku" name="sku" />

      </div>

    )
  }
}

You imported App instead of ThemeContext.

use import { ThemeContext } from '../App.js;

Here problem:

componentDidMount() {
    console.log(this.context);
}

Here can't find this.context variable.

static contextType = 

To

const contextType = 
发布评论

评论列表(0)

  1. 暂无评论