Setting Videotitel via useState in React Native.

huangapple go评论63阅读模式
英文:

Setting Videotitel via useState in React native

问题

I'm here to provide the translation:

我正在尝试在React Native中创建一个动态生成的视频视图。我使用参数lettermode调用了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);

Setting Videotitel via useState in React Native.

英文:

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);

Setting Videotitel via useState in React Native.

答案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;

huangapple
  • 本文由 发表于 2023年5月18日 05:52:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/76276434.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定