我正在读奥的《学习react 》.在这篇文章中,他们指出,我们可以通过创建一个名为"组件创建函数"的函数来创建组件,在这个函数中,我们可以提供所需的参数作为second parameterReact.createElement个函数.我也这么做了,但我没有得到任何结果.创建这样一个组件创建函数的最佳方法是什么?

var a = ['warm water', 'put egg to boil', 'boil finishes', 'scrape off shell'],
  b = 'making eggs',
  recipeArray = [],
  name

function recipeWriter() {
  return React.createElement(
    'section', {
      className: 'recipe'
    },
    React.createElement('h3', {
      className: 'recipeName',
      key: 0
    }, name),
    recipeArray.map((i, j) =>
      React.createElement('li', {
        key: j
      }, i)
    )
  )
}

let el = ReactDOM.render(
  React.createElement(recipeWriter, {
    recipeArray: a,
    name: b
  }, null), document.getElementById('root'))
#root {
  background-color: cornsilk;
  font: bold 1em monospace
}

.recipe {
  background-color: aqua;
  margin: 30px;
}

.recipeName {
  font: 2em monospace;
  color: red;
}

.steps {
  color: darkgreen
}
<div id="root"></div>
<script src="https://unpkg.com/react@16/umd/react.development.js" crossorigin></script>
<script src="https://unpkg.com/react-dom@16/umd/react-dom.development.js" crossorigin></script>

I want my output like this ->

var recipeArray = ['warm water', 'put egg to boil', 'boil finishes', 'scrape off shell'],
  name = 'making eggs'

function recipeWriter() {
  return React.createElement(
    'section', {
      className: 'recipe'
    },
    React.createElement('h3', {
      className: 'recipeName',
      key: 0
    }, name),
    recipeArray.map((i, j) =>
      React.createElement('li', {
        key: j
      }, i)
    )
  )
}

let el = ReactDOM.render(
  React.createElement(recipeWriter, null, null), document.getElementById('root'))
#root {
  background-color: cornsilk;
  font: bold 1em monospace
}

.recipe {
  background-color: aqua;
  margin: 30px;
}

.recipeName {
  font: 2em monospace;
  color: red;
}

.steps {
  color: darkgreen
}
<div id="root"></div>
<script src="https://unpkg.com/react@18/umd/react.development.js" crossorigin></script>
<script src="https://unpkg.com/react-dom@18/umd/react-dom.development.js" crossorigin></script>

推荐答案

您缺少prop中传递给recipeWriter组件的变量.

function recipeWriter(props) {
   const recipeArray = props.recipeArray;
   const name = props.name
}

或者更简单地用destructuring

function recipeWriter({ recipeArray, name }) {
}

Also you can remove the recipeArray and name from the top of your file as they are available in the component


无论何时调用React.createElement,第二个参数是props.例如,在您的代码中:

React.createElement(recipeWriter, {
    recipeArray: a,
    name: b
  }, null)

在这里,您将recipeArrayname传递给您的组件.使用功能组件的第一个参数可以访问这些props .

参见documentation for React.createElement

Javascript相关问答推荐

对象和数字减法会抵消浏览器js中的数字

成帧器运动中的运动组件为何以收件箱开始?

在贝塞尔曲线的直线上找不到交叉点:(使用@Pomax的bezier.js)

Bootstrap动态选项卡在切换选项卡后保持活动状态,导致元素堆叠

Phaser 3 console. log()特定游戏角色的瓷砖属性

使用i18next在React中不重新加载翻译动态数据的问题

Snowflake JavaScript存储过程返回成功,尽管预期失败

将字符串UTC Date转换为ngx—counting leftTime配置值的数字

当作为表达式调用时,如何解析方法decorator 的签名?

jQuery s data()[storage object]在vanilla JavaScript中?'

Chart.js 4.4.2,当悬停在一个数据点上时,如何在工具提示中拥有多个数据点/标签?

并不是所有的iframe都被更换

如何解决useState错误—setSelect Image不是函数''

在WordPress中使用带有WPCode的Java代码片段时出现意外令牌错误

WhatsApp Cloud API上载问题:由于MIME类型不正确而导致接收&Quot;INVALID_REQUEST";错误

检索相加到点的子项

变量在导入到Vite中的另一个js文件时成为常量.

用于部分字符串的JavaScript数组搜索

如何让SVG图标在被点击和访问后改变 colored颜色 ,并在被访问后取消点击时恢复到原来的 colored颜色 ?

AstroJS混合模式服务器终结点返回404