React上下文在子组件中未定义

huangapple go评论75阅读模式
英文:

React context undefined in child component

问题

以下是你要翻译的代码部分:

I'm new in react and I try to use a context for passing current username, I follow the tutorial, but it seems that my context is always undefined, can someone explain what I'm doing wrong?

const AppContext = React.createContext({});
class App extends React.Component {
  componentDidMount() {
    this.setState({
      User: {
        Username: "Guest",
        UserType: {
          Description: "Guest"
        }
      }
    });
  }
  render() {
    return (<div className="App">
      <AppContext.Provider value={this.state}>
        <Toolbar/>
        <!--This work fine-->
        <h3>{this.state.User.Username}</h3>
      </AppContext.Provider>
    </div>);
  }
}
class Toolbar extends React.Component {
  constructor(props, context) {
    super(props);
  }
  render() {
    return (
    <header className="toolbar">
      <!-- print the username -->
    </header>
    );
  }
}
export default Toolbar;

你尝试过的解决方案:

  1. <h3>{this.context.Username}</h3>

  2. <h3>{this.context.User.Username}</h3>

  3. this is something I found on internet, but it gives me "Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: undefined":

    <AppContext.Consumer>
      {(value) => (
        <p>Inside consumer {value}</p>
      )}
    </AppContext.Consumer>
    
英文:

I'm new in react and I try to use a context for passing current username, I follow the tutorial, but it seems that my context is always undefined, can someone explain what I'm doing wrong?

const AppContext = React.createContext({});
class App extends React.Component {
  componentDidMount() {
    this.setState({
      User: {
        Username: &quot;Guest&quot;,
        UserType: {
          Description: &quot;Guest&quot;
        }
      }
    });
  }
  render() {
    return (&lt;div className=&quot;App&quot;&gt;
      &lt;AppContext.Provider value={this.state}&gt;
        &lt;Toolbar/&gt;
        &lt;!--This work fine--&gt;
        &lt;h3&gt;{this.state.User.Username}&lt;/h3&gt;
      &lt;/AppContext.Provider&gt;
    &lt;/div&gt;);
  }
}
class Toolbar extends React.Component {
  constructor(props, context) {
    super(props);
  }
  render() {
    return (
    &lt;header className=&quot;toolbar&quot;&gt;
      &lt;!-- print the username --&gt;
    &lt;/header&gt;
    );
  }
}
export default Toolbar;

I tried these solutions:

  1. &lt;h3&gt;{this.context.Username}&lt;/h3&gt;

  2. &lt;h3&gt;{this.context.User.Username}&lt;/h3&gt;

  3. this is something I found on internet, but it gives me "Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: undefined":

    <AppContext.Consumer>
    {(value) => (
    <p>Inside consumer {value}</p>
    )}
    </AppContext.Consumer>

答案1

得分: 1

contextType 分配给读取上下文。

使用这个属性可以让您使用 this.context 消费最近的上下文类型的当前值。

class Toolbar extends React.Component {

  static contextType = AppContext;
  render() {
    return (
      <header className="toolbar">
        <!-- 打印用户名 -->
      </header>
    )
  }
}
英文:

Assign a contextType to read the context.

> Using this property lets you consume the nearest current value of that Context type using this.context.

class Toolbar extends React.Component {

  static contextType = AppContext;
  render() {
    return (
      &lt;header className=&quot;toolbar&quot;&gt;
        &lt;!-- print the username --&gt;
      &lt;/header&gt;
    )
  }
}

huangapple
  • 本文由 发表于 2023年3月15日 20:51:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/75744966.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定