You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

React JS报错:'Message' is not defined react/jsx-no-undef 求助

Fixing the 'Message' is not defined Error in Your React Messaging App

Hey there! Let's break down why you're hitting that 'Message' is not defined error and how to fix it. This is super common for React beginners, so don't worry—we'll get it sorted quickly.

Common Causes & Solutions:

  • You forgot to import the Message component
    If your Message component lives in a separate file (like Message.js), you need to import it at the top of your App.js file. Make sure the path matches where your component is stored. For example:

    // At the top of App.js
    import Message from './Message'; // Adjust the path if needed, e.g., './components/Message'
    
  • The Message component is defined after you use it
    If you're writing the Message component in the same App.js file, you have to define it before the code where you map over messages. React reads code top-to-bottom, so it can't use something that hasn't been created yet. Here's the correct order:

    // First define the Message component
    function Message({ username, text }) {
      return (
        <div className="message-bubble">
          <span className="username">{username}:</span>
          <span className="message-text">{text}</span>
        </div>
      );
    }
    
    // Then use it in your App component
    function App() {
      const messages = [/* your message data here */];
      return (
        <div className="app-container">
          {messages.map(message => (
            <Message 
              username={message.username} 
              text={message.text} 
              key={message.id} // Always add a unique key for mapped list items!
            />
          ))}
        </div>
      );
    }
    
  • Typos or spacing issues in the JSX tag
    Looking at your code snippet: < Message username = { message.username } text = { message.text } />
    The space between < and Message is a problem! React treats tags with this spacing as regular HTML elements (not custom React components). Remove the space, and double-check the component name is capitalized (React requires component names to start with a capital letter to tell them apart from HTML tags). The corrected tag should look like:

    <Message username={message.username} text={message.text} />
    

Quick Recap:

Double-check that you've imported or defined the Message component properly, fixed any typos/spacing in the JSX tag, and added a unique key prop to your mapped elements. That should resolve the compilation error.

内容的提问来源于stack exchange,提问作者Mrunmai Dahare

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.08 19:08:10