'react native useRef not working: TypeError: undefined is not an object (evaluating
i want to call methd _getRoomList() from child(named RecentScreen) when parent(namedHomeScreen) already. Here is my code:
home
export default function HomeScreen() {
const headerChildRef = React.useRef()
const _getMasterInfoApi = () => {
ApiHelper.getData(ApiConstant.GET_MASTER_INFO,
'_getMasterInfoApi')
.then(data => {
....
headerChildRef.current._getRoomList() => i want to get roomlist from here
})
};
useEffect(() => {
console.log('home')
_getMasterInfoApi()
}, [])
here is HomeScreen view and config ref
here is RecentScreen:
export default function RecentlyScreen({ref}) {
const {masterInfo} = React.useContext(MasterInfoContext)
let currentUser = masterInfo?.CurrentUser
const [roomList, setRoomList] = React.useState([])
_getRoomList = () => {
const getRoomUrl = currentUser?.Server.Address + ApiConstant.GET_ROOM_LIST
console.log('getRoomUrl', getRoomUrl)
ApiHelper.postData(getRoomUrl,
{
'Skip' : 0,
'Take' : 20
},
'_getRoomList')
.then(data => {
const rooms = data.Data
setRoomList(rooms)
})
}
But i got this error:
TypeError: undefined is not an object (evaluating 'headerChildRef.current._getRoomList')
can anyone guide me to know the problem? Thanks
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|

