英文:
Setting Videotitel via useState in React native
问题
I'm here to provide the translation:
我正在尝试在React Native中创建一个动态生成的视频视图。我使用参数letter
和mode
调用了Play
组件。从数组中找到的选择的视频是正确的。请查看控制台日志。但是videoTitel
始终是第一次调用时的视频。所以,只有在我改变setVideoTitle
,例如使用按钮的onclick
事件时,标题才会更改。
export default function Play ({ navigation, route}) {
let letter = route.params.Letter;
let mode = route.params.Mode;
const video = useRef(null);
const [selection, setSelection] = useState(0);
const Videos = require('../components/Videos');
const selectedVideo = Videos.Array.find(V => V.name === letter.toString());
console.log(selectedVideo)
const [videoTitel, setVideoTitel] = useState(selectedVideo.Front[mode]);
console.log("VideoTitel: "+ videoTitel)
const [videoSpeed, setVideoSpeed] = useState(1.0);
英文:
I'm trying to create a dynamically generated video view in React native. I call the side Play
with the parameters letter
and mode
. The selected video found out of an array is correct. See console log. But the videoTitel
itself is always the video from the first side call. So it's not updating when I call the side again. The title only changes if I change setVideoTitle
, e.g., with a button onclick
.
export default function Play ({ navigation, route}) {
let letter = route.params.Letter;
let mode = route.params.Mode;
const video = useRef(null);
const [selection, setSelection] = useState(0);
const Videos = require('../components/Videos');
const selectedVideo = Videos.Array.find(V => V.name === letter.toString());
console.log(selectedVideo)
const [videoTitel, setVideoTitel] = useState(selectedVideo.Front[mode]);
console.log("VideoTitel: "+ videoTitel)
const [videoSpeed, setVideoSpeed] = useState(1.0);
答案1
得分: 1
我不知道你如何使用这个Play函数,但试试这个:
```js
const Videos = require('../components/Videos');
function Play ({ navigation, route }) {
const video = useRef(null);
let letter = route.params.Letter;
let mode = route.params.Mode;
const [selection, setSelection] = useState(0);
const [videoSpeed, setVideoSpeed] = useState(1);
const [videoTitle, setVideoTitle] = useState();
const selectedVideo = Videos.Array.find(V => V.name === letter.toString());
useEffect(() => {
if(selectedVideo && mode) {
setVideoTitle(selectedVideo.Front[mode]);
console.log('Selected Video:', selectedVideo);
console.log('Video Title: ', videoTitle);
}
},[selectedVideo, mode]);
...
};
export default Play;
英文:
I don't know how you are using this Play function but try this:
const Videos = require('../components/Videos');
function Play ({ navigation, route }) {
const video = useRef(null);
let letter = route.params.Letter;
let mode = route.params.Mode;
const [selection, setSelection] = useState(0);
const [videoSpeed, setVideoSpeed] = useState(1);
const [videoTitel, setVideoTitel] = useState();
const selectedVideo = Videos.Array.find(V => V.name === letter.toString());
useEffect(() => {
if(selectedVideo && mode) {
setVideoTitle(selectedVideo.Front[mode]);
console.log('Selected Video:', selectedVideo);
console.log('VideoTitel: ', videoTitel);
}
},[selectedVideo, mode]);
...
};
export default Play;
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论