我正在尝试将props.Name
的值传递给其他脚本,但正在给我undefined
。此代码有什么问题?
export default function Tile(props) {
const videoEl = useRef(null);
const audioEl = useRef(null);
const nameEl = useRef(null);
/**
* When video track changes, update video srcObject
*/
useEffect(() => {
videoEl.current &&
(videoEl.current.srcObject = new MediaStream([props.videoTrack]));
}, [props.videoTrack]);
useEffect(() => {
nameEl.current &&
(nameEl.current = "Mohd Jamshaid");
}, [props.Name]);
/**
* When audio track changes, update audio srcObject
*/
useEffect(() => {
audioEl.current &&
(audioEl.current.srcObject = new MediaStream([props.audioTrack]));
}, [props.audioTrack]);
function getLoadingComponent() {
return props.isLoading && <p className="loading">Loading...</p>;
}
function getNameComponent() {
//var x = Math.floor((Math.random() * 9999999));
return props.Name && <p className="name" ref={nameEl}>{props.Name}</p>;
}
function getVideoComponent() {
return (
props.videoTrack && <video autoPlay muted playsInline ref={videoEl} />
);
}
function getAudioComponent() {
return (
!props.isLocalPerson &&
props.audioTrack && <audio autoPlay playsInline ref={audioEl} />
);
}
function getClassNames() {
let classNames = "tile";
classNames += props.isLarge ? " large" : " small";
props.isLocalPerson && (classNames += " local");
return classNames;
}
return (
<div className={getClassNames()}>
<div className="background" />
{getLoadingComponent()}
{getVideoComponent()}
{getAudioComponent()}
{getNameComponent()}
</div>
);
}
此脚本
import React, { useEffect, useContext, useReducer } from "react";
import "./Call.css";
import Tile from "../Tile/Tile";
import CallObjectContext from "../../CallObjectContext";
import CallMessage from "../CallMessage/CallMessage";
import {
initialCallState,
CLICK_ALLOW_TIMEOUT,
PARTICIPANTS_CHANGE,
CAM_OR_MIC_ERROR,
FATAL_ERROR,
callReducer,
isLocal,
isScreenShare,
containsScreenShare,
getMessage
} from "./callState";
import { logDailyEvent } from "../../logUtils";
export default function Call() {
const callObject = useContext(CallObjectContext);
const [callState, dispatch] = useReducer(callReducer, initialCallState);
/**
* Start listening for participant changes, when the callObject is set.
*/
useEffect(() => {
if (!callObject) return;
const events = [
"participant-joined",
"participant-updated",
"participant-left"
];
function handleNewParticipantsState(event) {
event && logDailyEvent(event);
dispatch({
type: PARTICIPANTS_CHANGE,
participants: callObject.participants()
});
}
// Use initial state
handleNewParticipantsState();
// Listen for changes in state
for (const event of events) {
callObject.on(event, handleNewParticipantsState);
}
// Stop listening for changes in state
return function cleanup() {
for (const event of events) {
callObject.off(event, handleNewParticipantsState);
}
};
}, [callObject]);
/**
* Start listening for call errors, when the callObject is set.
*/
useEffect(() => {
if (!callObject) return;
function handleCameraErrorEvent(event) {
logDailyEvent(event);
dispatch({
type: CAM_OR_MIC_ERROR,
message:
(event && event.errorMsg && event.errorMsg.errorMsg) || "Unknown"
});
}
// We're making an assumption here: there is no camera error when callObject
// is first assigned.
callObject.on("camera-error", handleCameraErrorEvent);
return function cleanup() {
callObject.off("camera-error", handleCameraErrorEvent);
};
}, [callObject]);
/**
* Start listening for fatal errors, when the callObject is set.
*/
useEffect(() => {
if (!callObject) return;
function handleErrorEvent(e) {
logDailyEvent(e);
dispatch({
type: FATAL_ERROR,
message: (e && e.errorMsg) || "Unknown"
});
}
// We're making an assumption here: there is no error when callObject is
// first assigned.
callObject.on("error", handleErrorEvent);
return function cleanup() {
callObject.off("error", handleErrorEvent);
};
}, [callObject]);
/**
* Start a timer to show the "click allow" message, when the component mounts.
*/
useEffect(() => {
const t = setTimeout(() => {
dispatch({ type: CLICK_ALLOW_TIMEOUT });
}, 2500);
return function cleanup() {
clearTimeout(t);
};
}, []);
function getTiles() {
let largeTiles = [];
let smallTiles = [];
Object.entries(callState.callItems).forEach(([id, callItem]) => {
const isLarge =
isScreenShare(id) ||
(!isLocal(id) && !containsScreenShare(callState.callItems));
const tile = (
<Tile
key={id}
videoTrack={callItem.videoTrack}
audioTrack={callItem.audioTrack}
isLocalPerson={isLocal(id)}
isLarge={isLarge}
isLoading={callItem.isLoading}
Name={callItem.Name}
/>
);
console.log(callItem.Name);
if (isLarge) {
largeTiles.push(tile);
} else {
smallTiles.push(tile);
}
});
return [largeTiles, smallTiles];
}
const [largeTiles, smallTiles] = getTiles();
const message = getMessage(callState);
return (
<div className="call">
<div className="large-tiles">
{!message
? largeTiles
: null /* Avoid showing large tiles to make room for the message */}
</div>
<div className="small-tiles">{smallTiles}</div>
{message && (
<CallMessage
header={message.header}
detail={message.detail}
isError={message.isError}
/>
)}
</div>
);
}