TypeError: 无法读取 null 的属性(读取 ‘type’)

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

TypeError: Cannot read properties of null (reading 'type')

问题

我想在React应用中打印警报,但我无法打印,因为这个问题。当键入{props.alert.type}和{props.alert.msg}时,代码不起作用。所以请帮助我解决这个问题。

import React, { useState } from 'react';
import Alert from './component/Alert';
import TextFrom from './component/TextFrom';

function App() {
  const [mode, setMode] = useState('light'); // 是否启用暗模式
  const [alert, setAlert] = useState(null);

  const showAlert = (message, type) => {
    setAlert({
      msg: message,
      type: type
    });
  };

  const toggleMode = () => {
    if (mode === 'light') {
      setMode('dark');
      document.body.style.backgroundColor = '#14142c';
      showAlert("Dark mode is enabled", "success");
    } else {
      setMode('light');
      document.body.style.backgroundColor = 'white';
      showAlert("Light mode is enabled", "success");
    }
  };

  return (
    <>
      <Navbar title="TextUtils" mode={mode} toggleMode={toggleMode} />
      <Alert alert={alert} />
      <div className='container'>
        <TextFrom heading="Enter the text given below" mode={mode} />
      </div>
    </>
  );
}

export default App;
import React from 'react';

function Alert(props) {
  return (
    <div>
      {props.alert && (
        <div className="alert alert-warning alert-dismissible fade show " role="alert">
          <strong>{props.alert.type}:</strong> {props.alert.msg}
          <button type="button" className="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
        </div>
      )}
    </div>
  );
}

export default Alert;

请注意,我已经修复了一些拼写错误,并稍微改进了代码以使其更容易理解。希望这可以帮助您解决问题。如果问题仍然存在,请提供更多详细信息。

英文:

i want to print Alert in react app but i can't able to print coz this.
when type {props.alert.type} as well as {props.alert.mag} the code not working .
so please help m get stuck in this code

import React, { useState } from &#39;react&#39;;
import Alert from &#39;./component/Alert&#39;;
import TextFrom from &#39;./component/TextFrom&#39;;


function App() {
  const[mode,setMode] = useState(&#39;light&#39;);//wheather dark mode is enabled or not
  const [alert, setAlert] = useState(null);

 const showAlert=((message,type)=&gt;{
      setAlert( {
         msg:message,
         type:type
       }) 


const toggleMode=()=&gt;{
    if(mode === &#39;light&#39;){
      setMode(&#39;dark&#39;)
      document.body.style.backgroundColor=&#39;#14142c&#39;;
      showAlert(&quot;Dark modes enabled&quot;, &quot;success&quot;);
    }
    else{
      setMode(&#39;light&#39;)
      document.body.style.backgroundColor=&#39;white&#39;;
      showAlert(&quot;light mode is enabled&quot;, &quot;success&quot;);
    
  }
}

 return (
        &lt;&gt;      
        &lt;Navbar title=&quot;TextUtils&quot; mode={mode} toggleMode={toggleMode} /&gt; 
        &lt;Alert  alert={alert}/&gt;
        &lt;div className=&#39;container&#39;&gt;
        &lt;TextFrom heading=&quot;Enter the text given below&quot; mode={mode}/&gt;
        &lt;/div&gt;
          &lt;/&gt;
     );
  }    
  

export default App;

// upper code is this for &#39;App&#39; main page. 
// bottom is another page //
import React from &#39;react&#39;

function Alert(props) {
    return (
        &lt;div&gt;
          props.alert &amp;&amp; &lt;div className=&quot;alert alert-warning alert-dismissible fade show &quot; role=&quot;alert&quot;&gt;
                &lt;strong&gt;{props.alert.type}&lt;/strong&gt;: {props.alert.msg}
                &lt;button type=&quot;button&quot; className=&quot;btn-close&quot; data-bs-dismiss=&quot;alert&quot; aria-label=&quot;Close&quot;&gt;&lt;/button&gt;
            &lt;/div&gt;
        &lt;/div&gt;
    )
}

export default Alert

so please help me to resole the code so i can continue the work on the website .

this output is showing when m run this app given below the screen.

enter image description here

答案1

得分: 1

在你的Alert组件中似乎存在语法错误,缺少了一些花括号。

请使用以下行替换Alert组件中的以下行:

{props.alert && <div className="alert alert-warning alert-dismissible fade show " role="alert">}

这应该解决问题并正确渲染警报。

英文:

There seems to be a syntax error in your Alert component, it's missing some curly braces.

Replace the following line in your Alert component:

props.alert &amp;&amp; &lt;div className=&quot;alert alert-warning alert-dismissible fade show &quot; role=&quot;alert&quot;&gt;

With the following line:

{props.alert &amp;&amp; &lt;div className=&quot;alert alert-warning alert-dismissible fade show &quot; role=&quot;alert&quot;&gt;

This should fix the issue and render the alert properly.

huangapple
  • 本文由 发表于 2023年3月12日 12:55:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/75711115.html
匿名

发表评论

匿名网友

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

确定