我的代码在运行时,我测试了一个React网站,但我的功能未显示出来。

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

I test a website in react and my functions are not showing when I run my code

问题

  1. function banner() {
  2. const title = "c'est pur 😄";
  3. return (<h1>{title}</h1>);
  4. }
  5. function cart() {
  6. const prixbedo = 10;
  7. const prixhero = 20;
  8. const prixcoc = 70;
  9. return (
  10. <div>
  11. <h2>Panier</h2>
  12. <ul>
  13. <li>bedo : {prixbedo}$</li>
  14. <li>héroïne : {prixhero}$</li>
  15. <li>cocaïne : {prixcoc}$</li>
  16. </ul>
  17. </div>
  18. );
  19. }
  20. ReactDOM.render(
  21. <div>
  22. <banner />
  23. <cart />
  24. </div>,
  25. document.getElementById("root")
  26. );

i declared my file .js in my file .html when I test with simple function is good I see on my website the result but with this code nothing happens

  1. <details>
  2. <summary>英文:</summary>
  3. `

function bannner() {
const title = "c'est pur 😎"
return (<h1>{title}</h1>)
}

function cart() {
const prixbedo = 10
const prixhero = 20
const prixcoc = 70
return (<div>
<h2>Panier</h2>
<ul>
<li>bedo : {prixbedo}$</li>
<li>heroïne : {pixhero}$</li>
<li>cocaïne : {prixcoc}$</li>
</ul>
</div>)
}

ReactDOM.render(<div><banner /><cart /></div>, document.getElementById("root"))

  1. i declared my file .js in my file .html when I test with simple function is good I see on my website the result but with this code nothing happens
  2. </details>
  3. # 答案1
  4. **得分**: 0
  5. ```
  6. 需要创建根元素。
  7. ```
  8. function bannner() {
  9. const title = "c'est pur 😆";
  10. return (<h1>{title}</h1>);
  11. }
  12. function cart() {
  13. const prixbedo = 10;
  14. const prixhero = 20;
  15. const prixcoc = 70;
  16. return (
  17. <div>
  18. <h2>Panier</h2>
  19. <ul>
  20. <li>bedo : {prixbedo}$</li>
  21. <li>héroïne : {prixhero}$</li>
  22. <li>cocaïne : {prixcoc}$</li>
  23. </ul>
  24. </div>
  25. );
  26. }
  27. const root = ReactDOM.createRoot(document.getElementById("root"));
  28. root.render(<div><banner /><cart /></div>);
  29. ```
  30. <details>
  31. <summary>英文:</summary>
  32. You need to create root.
  33. ```
  34. function bannner() {
  35. const title = &quot;c&#39;est pur &#128526;&quot;
  36. return (&lt;h1&gt;{title}&lt;/h1&gt;)
  37. }
  38. function cart() {
  39. const prixbedo = 10
  40. const prixhero = 20
  41. const prixcoc = 70
  42. return (&lt;div&gt;
  43. &lt;h2&gt;Panier&lt;/h2&gt;
  44. &lt;ul&gt;
  45. &lt;li&gt;bedo : {prixbedo}$&lt;/li&gt;
  46. &lt;li&gt;hero&#239;ne : {pixhero}$&lt;/li&gt;
  47. &lt;li&gt;coca&#239;ne : {prixcoc}$&lt;/li&gt;
  48. &lt;/ul&gt;
  49. &lt;/div&gt;)
  50. }
  51. const root = ReactDOM.createRoot( document.getElementById(&quot;root&quot;))
  52. root.render(&lt;div&gt;&lt;banner /&gt;&lt;cart /&gt;&lt;/div&gt;)
  53. </details>
  54. # 答案2
  55. **得分**: 0
  56. 请注意,以下是代码的翻译部分:
  57. ```javascript
  58. function Banner() {
  59. const title = "c'est pur 😆"
  60. return <h1>{title}</h1>
  61. }
  62. function Cart() {
  63. const prixbedo = 10
  64. const prixhero = 20
  65. const prixcoc = 70
  66. return (
  67. <div>
  68. <h2>Panier</h2>
  69. <ul>
  70. <li>bedo : {prixbedo}$</li>
  71. <li>héroïne : {prixhero}$</li>
  72. <li>cocaïne : {prixcoc}$</li>
  73. </ul>
  74. </div>
  75. )
  76. }
  77. ReactDOM.render(
  78. <div>
  79. <Banner />
  80. <Cart />
  81. </div>,
  82. document.getElementById("root")
  83. )
  84. ```
  85. 希望这有所帮助。如果你需要进一步的翻译,请告诉我。
  86. <details>
  87. <summary>英文:</summary>
  88. You should not write the name of the tale in lowercase.
  89. Causes an error in the function rendering.
  90. Write as follows:
  91. &lt;!-- begin snippet: js hide: false console: true babel: false --&gt;
  92. &lt;!-- language: lang-js --&gt;
  93. function Banner() {
  94. const title = &quot;c&#39;est pur &#128526;&quot;
  95. return &lt;h1&gt;{title}&lt;/h1&gt;
  96. }
  97. function Cart() {
  98. const prixbedo = 10
  99. const prixhero = 20
  100. const prixcoc = 70
  101. return (
  102. &lt;div&gt;
  103. &lt;h2&gt;Panier&lt;/h2&gt;
  104. &lt;ul&gt;
  105. &lt;li&gt;bedo : {prixbedo}$&lt;/li&gt;
  106. &lt;li&gt;hero&#239;ne : {prixhero}$&lt;/li&gt;
  107. &lt;li&gt;coca&#239;ne : {prixcoc}$&lt;/li&gt;
  108. &lt;/ul&gt;
  109. &lt;/div&gt;
  110. )
  111. }
  112. ReactDOM.render(
  113. &lt;div&gt;
  114. &lt;Banner /&gt;
  115. &lt;Cart /&gt;
  116. &lt;/div&gt;,
  117. document.getElementById(&quot;root&quot;)
  118. )
  119. &lt;!-- end snippet --&gt;
  120. </details>
  121. # 答案3
  122. **得分**: 0
  123. 我认为你的组件没有显示出来是因为你像其他人指出的一样使用小写来编写你的函数。我只是想提供更多的上下文。
  124. 根据React文档[这里][1],在JSX中,小写标签名称被视为HTML标签。例如:
  125. `<cart />` 编译为 React.createElement('cart')(HTML标签)。
  126. `<Cart />` 编译为 React.createElement(Cart)(React组件)。
  127. 因此,因为`<cart />`不是一个被识别的标签,所以它不会显示出来。希望这有所帮助。
  128. [1]: https://reactjs.org/docs/jsx-in-depth.html#html-tags-vs.-react-components
  129. <details>
  130. <summary>英文:</summary>
  131. I think your component not showing up is because you used lower case to write your functions just like other people have pointed out. I just wanted to give more context.
  132. According to the react docs [here][1], in JSX, lower-case tag names are considered to be HTML tags. eg
  133. `&lt;cart /&gt;` compiles to React.createElement(&#39;cart&#39;) (html tag).
  134. `&lt;Cart /&gt;` compiles to React.createElement(Cart) (a react component)
  135. So because `&lt;cart /&gt;` is not a recognised tag, it doesn&#39;t show. Hope this helps
  136. [1]: https://reactjs.org/docs/jsx-in-depth.html#html-tags-vs.-react-components
  137. </details>

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

发表评论

匿名网友

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

确定