如何在数组的.map中调用函数?数组、函数、如何在、map

2023-09-03 12:41:56 作者:刪了回忆、却刪不了痛

我正在通过道具传递一个函数,该函数用大写字母表示要映射的一些项。我在item.creator的项部分收到一个错误。我只是想知道为什么我会收到这个错误,而不能直接调用映射内部的函数。感谢您的帮助。

错误消息为行分析错误:意外标记,应为","。

javascript数组的map filter reduce forEach every some函数用法和区别

父组件

export default function MainContent() {
  const techContent = useSelector(displayTechContent);
  const designContent = useSelector(displayDesignContent);

  const makeCapital = (words) => words.replace(/^w/, (c) => c.toUpperCase());

  return (
    <div className="container">
      <div className="topics-list">
        <div className="topic-row mb-5">
          <h2 className="topic-heading mb-4">Software Development</h2>

          <ContentCard data={techContent} capitalize={makeCapital} />
        </div>

子组件

export default (props) => (
  <div>
    <div className="resource-list">
      {props.data.map((item) => (
        <a key={item.id} href={item.link} className="resource-card-link mr-3">
          <div className="card resource-card mb-2">
            <div className="card-header">
              <h4 className="resource-title">{item.title}</h4>
              <span className="resource-creator">by: ***{props.capitalize({item.creator})}***.</span> <--this function
            </div>

            <div className="card-body py-3">
              <div className="resource-description mb-2">
                {item.description}
              </div>
              <div className="resource-type mb-2">
                <i className="fas fa-book"></i> {item.type}
              </div>

推荐答案

item.creator的花括号是多余的。

export default (props) => (
  <div>
    <div className="resource-list">
      {props.data.map((item) => (
        <a key={item.id} href={item.link} className="resource-card-link mr-3">
          <div className="card resource-card mb-2">
            <div className="card-header">
              <h4 className="resource-title">{item.title}</h4>
              <span className="resource-creator">by: ***{props.capitalize(item.creator)}***.</span> <--this function
            </div>

            <div className="card-body py-3">
              <div className="resource-description mb-2">
                {item.description}
              </div>
              <div className="resource-type mb-2">
                <i className="fas fa-book"></i> {item.type}
              </div>