首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >React context -未定义'contextType‘

React context -未定义'contextType‘
EN

Stack Overflow用户
提问于 2018-12-26 00:56:46
回答 3查看 9.9K关注 0票数 11

我使用了应该支持react Context的react-dom@16.6.1和react@16.6.1,并尝试运行一个与react-context相同的简单示例

app.js

代码语言:javascript
复制
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(没有上下文引用)

代码语言:javascript
复制
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>

        )
    }
}

和我想要使用上下文的SearchBar.js:

代码语言:javascript
复制
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>

    )
  }
}

如果我运行应用程序,我会在SearchBar.js中得到Line 44: 'contextType' is not defined no-undef,如果我删除这一行,当我记录this.context时,我会得到{}

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2018-12-26 01:12:16

您没有正确使用context,因为您需要将其定义为类的静态属性,并将其作为命名导入导入,因为您已将其导出为命名导入

代码语言:javascript
复制
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>

    )
  }
}
票数 11
EN

Stack Overflow用户

发布于 2018-12-26 01:10:31

您导入的是App而不是ThemeContext

使用import { ThemeContext } from '../App.js

票数 3
EN

Stack Overflow用户

发布于 2018-12-26 01:06:51

这里有个问题:

代码语言:javascript
复制
componentDidMount() {
    console.log(this.context);
}

这里找不到this.context变量。

代码语言:javascript
复制
static contextType = 

To

const contextType = 
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/53924140

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档