简体   繁体   English

在 React 组件中多次重复作为道具传递的函数

[英]Repeating a Function Passed as a Props in React Component Multiple Times

I'm Presently working on random Hexadecimal color generators.我目前正在研究随机十六进制颜色生成器。 I have my Hexa component and App component, I'm passing the Hexa component as a props in App.js and everything is working fine.我有我的 Hexa 组件和 App 组件,我将 Hexa 组件作为 App.js 中的道具传递,一切正常。 But the problem I'm facing is that I want my Hexa to appear multiple times on the browser rather than once that it's showing.但我面临的问题是我希望我的 Hexa 在浏览器上出现多次,而不是一次。 My code below.我的代码如下。

Hexa component六元组

import React from "react";

export default function Hexa(props) {
  return (
    <div>
      <div className="child_content">
        {" "}
        <h1>Random Colors</h1>
        <h2>Hexadecimal Colors</h2>
        <div
          className="background_div"
          style={{ backgroundColor: props.hexaColor() }}
        >
          <div className="hexa_center"> {props.hexaColor()} </div>
        </div>
      </div>
    </div>
  );
}

App.js应用程序.js

import React from "react";

import Hexa from "./Hexa";
import "./Style.css";

export default function App() {
  const hexaColor = () => {
    let str = "0123456789abcdef";
    let color = "";
    for (let i = 0; i < 6; i++) {
      let index = Math.floor(Math.random() * str.length);
      color += str[index];
    }
    return "#" + color;
  };

  return (
    <div className="container">
      <div className="child">
        <Hexa hexaColor={hexaColor} />
      </div>
    </div>
  );
}

it's so simple just render the hexa component multiple times with fake array.它是如此简单,只需使用假数组多次渲染 hexa 组件。

<div className='container'>
    <div className="child">
      {
        new Array(10).fill(0).map((item, i) => {
          return <Hexa  key={i} hexaColor={hexaColor}/>
        })
      }
    </div>
</div>

in case you need just the hexa color to be repeated, update the hexa component like that.如果您只需要重复六边形颜色,请像这样更新六边形组件。

function Hexa(props) {
  return (
    <div>
      <div className="child_content"> <h1>Random Colors</h1>
          <h2>Hexadecimal Colors</h2>
          {
            new Array(10).fill(0).map((item, i) => {
              return (
                <React.Fragment key={i}>
                <div  className="background_div" style={{ backgroundColor: props.hexaColor() }} >
                  <div className="hexa_center"> {props.hexaColor() } </div>
                </div>
                </React.Fragment>
              )
            })
          }
        
    </div>
    </div>
  )
}


function App() {
    
   const hexaColor = () => {
    let str = '0123456789abcdef'
    let color = ''
    for (let i = 0; i < 6; i++) {
      let index = Math.floor(Math.random() * str.length);
      color += str[index];
      console.log(color);
    }
    return '#' + color 
  }


    return (
      <div className='container'>
        <div className="child">
            <Hexa hexaColor={hexaColor}/>
        </div>
        
      </div>
    )
}

You should repeat your Hexa component.您应该重复您的 Hexa 组件。
I have solved like this:我是这样解决的:

Your App.js你的 App.js

import React from "react";
import Hexa from "./components/Hexa";

export default function App() {

  const hexaColor = () => {
    let str = "0123456789abcdef";
    let color = "";
    for (let i = 0; i < 6; i++) {
      let index = Math.floor(Math.random() * str.length);
      color += str[index];
    }
    return "#" + color;
  };

  const createManyHexaComp = (iteration) => {
    let result;
    for (let i = 0; i < iteration; i++) {
      result = <>
        {result}
        <Hexa hexaColor={hexaColor} />
      </>;
    }
    return result;

  }

  return (
    <div className="container">
      <div className="child">
        <div className="child_content">
          {" "}
          <h1>Random Colors</h1>
          <h2>Hexadecimal Colors</h2>
          {createManyHexaComp(5)}
        </div>
      </div>
    </div>
  );
}

Your Hexa component您的 Hexa 组件

import React from 'react';
export default function Hexa(props) {


    return (
        <div>
            <div
                className="background_div"
                style={{ backgroundColor: props.hexaColor() }}
            >
                <div className="hexa_center"> {props.hexaColor()} </div>
            </div>
        </div>
    );
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM