1

I am writing player UI in react-native with expo. There is a 3rd party library expo-av. And in examples section of that lib, react component initialize instance of mediaObject in useEffect and saves reference to it in ref (useRef). And handle events from mediaObject directly in react component via mediaObjectRef.setOnPlaybackStatusUpdate = status => {}

I wrote a Class Media, that wraps mediaObject and put there all logic about playing. And also on init() i pass react players' setState hook to this class. And there from onPlaybackStatusUpdate method I update react UI component. Is it normal? I can't find about this approach in google.

I avoid to put this logic to component, because I need to extend my Media class into MediaAdvanced with more logic, and rewrite there event handlers.

Rustam Apay
  • 551
  • 1
  • 4
  • 18
  • `setState` isn't a react hook. If you are referring to the state updater returned then yes, you *could* pass this around to other components to call, but then you leave it to other components to maintain your state invariant. Better to instead pass a callback around that the player component defines, this way the player component maintains control. – Drew Reese Feb 09 '21 at 21:41
  • yes, I mean [state, setState] = useState() . But I pass it not to other component, but to class instance that wraps 3rd party library (not react component, just some player.mediaObject instanse ) – Rustam Apay Feb 09 '21 at 21:54
  • Please include a [Minimal, Complete, and Reproducible Example](https://stackoverflow.com/help/minimal-reproducible-example) so we've a clearer idea what you are referring to. – Drew Reese Feb 09 '21 at 22:16
  • It is not bug report. It is just theoretical question: Is it ok or not: to send `setState` to class instance (not react component, just some custom OOP class instance). And it is hard to extract minimal code. – Rustam Apay Feb 10 '21 at 05:44

0 Answers0