why my react hook(usestate) not rendering?

miracle godwin

i'm using django restframework for my server side, i have fetch my datas on ReactJS, set it using "setPosts", consoled my response and i am getting my require response but when i try to render it in my return() block. i am not getting the data. rather i am having a blank page.

i am using a windows 11 and python 3.11.0

import React,{Component,useState,useEffect,useRef} from "react";
import user_img from '../1.jpeg';
import music_img from '../2.jpg';
import music from '../Victony.mp3';
import { Button } from "@mui/material";
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
import { faPlay,faPause,faForward,faBackward } from '@fortawesome/free-solid-svg-icons';
import WaveSurfer from 'wavesurfer.js';
import AudioPlayer from 'react-modern-audio-player';

import RegionsPlugin from "wavesurfer.js/dist/plugin/wavesurfer.regions.min";
import TimelinePlugin from "wavesurfer.js/dist/plugin/wavesurfer.timeline.min";
import CursorPlugin from "wavesurfer.js/dist/plugin/wavesurfer.cursor.min";

// import MyCustomPlugin from 'my-custom-plugin-path';




// const WaveFormOptions=ref=>({
//     barWidth: 3,
//     cursorWidth: 1,
//     container: ref,
//     // backend: 'WebAudio',
//     height: 80,
//     progressColor: '#2D5BFF',
//     responsive: true,
//     waveColor: '#EFEFEF',
//     cursorColor: 'transparent',
// });



const Home=()=>{

    const audio=document.querySelector('#audio')
    const music_container=document.querySelector('#music_container')

    const [image, setImage]=useState('')
    const [posts, setPosts]=useState([])
    const [icon,setIcon]=useState(faPlay)
    const[playing,setPlaying]=useState(false)
    // const progress=useRef()
    const[progress,SetCurrentProgress]=useState(0)
   
    



        function getAllPosts(){
            fetch(`http://127.0.0.1:8000/`)
            .then(response=>response.json())
            .then(data=>{
                console.log(data)
                setPosts(data)
            })
            
        }
        // console.log(posts.artist_name)

    // LOAD ALL POSTS
            // function soundrimage(){
            //     // setImage(music_img)
                
            // }

            useEffect(()=>{
                getAllPosts() 
                // console.log(this)
            },[])
        


    // LOAD AND CONTROL MUSIC
                function playSong(){
                    music_container.classList.add('play')
                    setIcon(faPause)
                    setPlaying(true)
                    audio.play()
                    
                }

                function pauseSong(){
                    music_container.classList.remove('play')
                    setIcon(faPlay)
                    setPlaying(true)
                    audio.pause()
                }

            function playPause(){
                let isplaying=music_container.classList.contains('play')
                if(isplaying){
                    pauseSong()
                }else{
                    playSong()
                }
            }


            const UpdateProgress=(e)=>{
                // console.log(e.target.duration)
                const {duration,currentTime}=e.target

                const ProgressPercent=(currentTime/duration)*100
                
                SetCurrentProgress(`${ProgressPercent}`)
                

            }

            // console.log(progress.current)

        
            const setProgress=(e)=>{
                const width=e.target.clientWidth
                // console.log(e)
                const clickX=e.nativeEvent.offsetX
                // console.log(clickX)
                const duration=audio.duration
                // console.log(duration)

                audio.currentTime=(clickX/width)*duration

            }

        
      


     


        // MUSIC INFO
        // const music_image= document.querySelector("#music_image")



        
       
        
       
   

    return(
        <body>
             
            
            <Navbar/>
            <main className="landing">





        {posts.map(post=>{
                        <div className="music_container" onClick={playPause} id="music_container">
                      
                                    
                        <img  id="music_image" src={'http://127.0.0.1:8000'+post.image} />

                        <button >
                            <FontAwesomeIcon icon={icon}  id="playBtn"  />
                        </button>
                        <audio src='' id="audio" onTimeUpdate={e=>UpdateProgress(e)}/>

                        <p className="caption">{post.title}</p>
                
                        <p>{post.title}</p>
                </div>
                    console.log(post.image)
                }

                )}

                  

                



                

            

            </main>
            <Play image={image} 
                playing={playing} 
                playpause={playPause}
                playicon={icon} 
                progress={progress} 
                setProgress={e=>setProgress(e)}
                 
                 />
           

        </body>
    )
}

export default Home
Sean Whelan

The issue here is with your map function. Try the following syntax:

{ posts.map((post) => (
    <div className="music_container" onClick={playPause} id="music_container">
        <img  id="music_image" src={'http://127.0.0.1:8000'+post.image} />
        <button>
            <FontAwesomeIcon icon={icon}  id="playBtn"  />
        </button>
        <audio src='' id="audio" onTimeUpdate={e=>UpdateProgress(e)}/>
        <p className="caption">{post.title}</p>
        <p>{post.title}</p>
    </div>
)) }

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Why are my buttons not rendering in react?

Why is my React component rendering twice?

Why is my React-Native component not rendering?

Why is React not rendering my map function?

Why are my radio inputs not rendering in React

Why is my React App Rendering Blank but Black?

Why is my React rendering twice after my state is declared?

Why is React Not Rendering my Data from my api?

why is my html dialog in react not rendering above my content?

Why is the data from Firebase not rendering in my FlatList (React Native)?

Why is React rendering my component over and over with an unchanged state?

Why is my React's component click method triggered at rendering time?

Why does my react component keep re-rendering?

Why is my React component that keeps track of state history not visually rendering?

Why isn't my navigation bar rendering in React?

Why is my React Component Not Rendering HTML, but logs correctly?

Why is my react component rendering twice on initial load?

Why is .card-header not rendering properly in my React Bootstrap setup?

My React component is re-rendering, cannot figure out why

Why React.Memo() keeps rendering my component

Why is my component is not rendering when I pass it props (react)?

Why is my react component not rendering from inside another?

why is my list not rendering?

Why is this map in react not rendering?

Why are my ValidationErrors not rendering anymore?

Why is my GLSL transformation not rendering?

Why react is not rendering updated states?

Why is this not rendering from a map, in react

Why is this react component rendering twice?

TOP Ranking

  1. 1

    Failed to listen on localhost:8000 (reason: Cannot assign requested address)

  2. 2

    Loopback Error: connect ECONNREFUSED 127.0.0.1:3306 (MAMP)

  3. 3

    How to import an asset in swift using Bundle.main.path() in a react-native native module

  4. 4

    pump.io port in URL

  5. 5

    Compiler error CS0246 (type or namespace not found) on using Ninject in ASP.NET vNext

  6. 6

    BigQuery - concatenate ignoring NULL

  7. 7

    ngClass error (Can't bind ngClass since it isn't a known property of div) in Angular 11.0.3

  8. 8

    ggplotly no applicable method for 'plotly_build' applied to an object of class "NULL" if statements

  9. 9

    Spring Boot JPA PostgreSQL Web App - Internal Authentication Error

  10. 10

    How to remove the extra space from right in a webview?

  11. 11

    java.lang.NullPointerException: Cannot read the array length because "<local3>" is null

  12. 12

    Jquery different data trapped from direct mousedown event and simulation via $(this).trigger('mousedown');

  13. 13

    flutter: dropdown item programmatically unselect problem

  14. 14

    How to use merge windows unallocated space into Ubuntu using GParted?

  15. 15

    Change dd-mm-yyyy date format of dataframe date column to yyyy-mm-dd

  16. 16

    Nuget add packages gives access denied errors

  17. 17

    Svchost high CPU from Microsoft.BingWeather app errors

  18. 18

    Can't pre-populate phone number and message body in SMS link on iPhones when SMS app is not running in the background

  19. 19

    12.04.3--- Dconf Editor won't show com>canonical>unity option

  20. 20

    Any way to remove trailing whitespace *FOR EDITED* lines in Eclipse [for Java]?

  21. 21

    maven-jaxb2-plugin cannot generate classes due to two declarations cause a collision in ObjectFactory class

HotTag

Archive