You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

28 lines
748 B

// 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 (
<div>
<button onClick={selectRandomStudent}>选择随机学生</button>
</div>
);
};
export default RandomSelector;