parent
11c806a021
commit
5af3fabe17
@ -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 (
|
||||
<div>
|
||||
<button onClick={selectRandomStudent}>选择随机学生</button>
|
||||
</div>
|
||||
);
|
||||
};
|
||||
|
||||
export default RandomSelector;
|
||||
|
Loading…
Reference in new issue