From 5af3fabe17385ace81dc097b90e49ff53abe9e6e Mon Sep 17 00:00:00 2001 From: pg75sc2al <2077276121@qq.com> Date: Sat, 12 Oct 2024 20:16:36 +0800 Subject: [PATCH] ADD file via upload --- RandomSelector.js | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 RandomSelector.js diff --git a/RandomSelector.js b/RandomSelector.js new file mode 100644 index 0000000..0c2f97e --- /dev/null +++ b/RandomSelector.js @@ -0,0 +1,27 @@ +// components/RandomSelector.js +import React from 'react'; + +const RandomSelector = ({ students, onSelect = () => { } }) => { + const selectRandomStudent = () => { + if (students.length === 0) return; + + const randomIndex = Math.floor(Math.random() * students.length); + const selectedStudent = students[randomIndex]; + + // 确保 onSelect 是一个函数 + if (typeof onSelect === 'function') { + onSelect(selectedStudent); + } else { + console.error('onSelect is not a function'); + } + }; + + return ( +