Why is my code not getting stored in the local storage? What is the error?

So I have this code where I want the code to be saved whenever I press the save button it is not happening, it isnt giving any errors but, it is not storing anything…

Here are the code, this is index js where I have the save button.

import { useNavigate, useParams } from "react-router-dom";
import { useRef, useState } from "react";
import "./playground.scss";
import codelogo from "./vlogo.png";
import { PlaygroundContext } from "../../Providers/PlaygroundProvider";
import { Navigate } from "react-router-dom";
import { EditorContainer } from "./EditorContainer";
import { useContext } from "react";
import Editor from "@monaco-editor/react";
export const PlaygroundScreen = () =>{


    const [code, setCode] = useState('');   
    const codeRef = useRef();
    const  {saveCode} = useContext(PlaygroundContext);
    const Navigate = useNavigate();

    const homepage = () => {
        Navigate(`/`);
    }

    const onSaveCode = () => {
        saveCode(fileId, id, codeRef.current)
        alert("Code Saved");
    }

   const params = useParams();
   console.log(params)
    const {fileId, id   } = params;  
    return(
        <div className="main-container">
            <div className="title-container">
                <div className="brand-container">
                <img src={ codelogo }></img>
                </div>
                <div className="button-container">
                <span class="material-symbols-outlined" onClick={onSaveCode}>save</span>
                <span class="material-symbols-outlined" onClick={homepage}>arrow_back</span>
                {/* <div className="purple-stuff"><br></br></div> */}
                </div>
            </div>

            <div className="code-container">
                <EditorContainer codeRef={codeRef} />
            </div>
        </div>
    );
}

This is the Editor-Container file with monaco running as the IDE

import { useRef, useState } from "react";
import "./EditorContainer.scss"
import Editor from "@monaco-editor/react";
import { PlaygroundContext } from "../../Providers/PlaygroundProvider";

const editorOptions={
    theme: 'vs-dark',
    wordWrap: 'on' 
    }

export const EditorContainer = ({codeRef}) =>{

    const [code, setCode] = useState('');
    // const codeRef = useRef();

    const onChangeCode = (newCode) =>{
        console.log(newCode);
        // setCode(newCode);
        codeRef.current = newCode;
    }

    return(
        <div className="root-editor-container">
                {/* <h1>YAHOOO</h1> */}
                <Editor 
                height={"100%"}
                language={"javascript"}
                options={editorOptions}
                theme={'vs-dark'}  
                onChange={onChangeCode}
                />
        </div>
    )
}

and finally this is the playground provider

import { createContext, useContext, useEffect, useState } from "react";
import {v4} from 'uuid';

export const PlaygroundContext = createContext();

// Data Array
const initialData = [
    {
        id:v4(),
        title: 'Project 1',
        files: [
            {
                title: 'index.js',
                id:v4(),
                language: 'javascript',
                code:':Ldkidm',
            },
            {
                title: 'dsa.js',
                id:v4(),
                language: 'javascript',
                code:':Ldkidm',
            },
            {
                title: 'right-container.js',
                id:v4(),
                language: 'javascript',
                code:':Ldkidmccccccc',
            }
            
        ]

    },
    {
        id:v4(),
        title: 'Project 2',
        files: [
            {
                title: 'index.js',
                id:v4(),
                language: 'javascript',
                code:':Ldkidm',
            },

            {
                title: 'routing.js',
                id:v4(),
                language: 'javascript',
                code:':Ldkidmccccccc',
            }
            
        ]

    }
    
    
];
//Main Func
export const PlaygroundProvider = ({children}) =>{

    const [folders, setFolders] = useState(() => {
        const localData = localStorage.getItem('data');
        if(localData){
            return JSON.parse(localData);
        }
        return initialData;
    });


    const editFolderTitle = (newFolderName, id) => {
        const updatedFoldersList = folders.map((folderItem) => {
            if(folderItem.id === id){
                folderItem.title = newFolderName;
            }
        })
        localStorage.setItem('data', JSON.stringify(updatedFoldersList))
        setFolders(updatedFoldersList);

    }

    const createNewPlayground = (newPlayground) => {
        // console.log ({newPlayground}, "inside pgprov")
        const {folderName, language} = newPlayground;
        const newFolders = [...folders];
        newFolders.push({
            id: v4(),
            title: folderName,
            files: []
        })
        localStorage.setItem('data', JSON.stringify(newFolders));
        setFolders(newFolders);
    }

    const createNewFile = (newFilesystem) => {
        const {fileName, language} = newFilesystem;
        const newFile = [...folders]
        newFile.push({
           id:v4(),
            title:fileName,
            files: [{
                id: v4(),
                title: fileName,
                language: language
            }]
            
        })

        const allFolders = [...folders, newFile] 

        localStorage.setItem('data', JSON.stringify(newFile));
        setFolders(newFile);

    }

    const deleteFolder = (id) => {
        const updatedFoldersList = folders.filter((folderItem) => {
            return folderItem.id !== id;
        })

        localStorage.setItem('data', JSON.stringify(updatedFoldersList));
        setFolders(updatedFoldersList);
    }


    const saveCode = (fileId, id, newCode) => {
        const newFolders = [...folders]
        for(let i=0 ; i < newFolders.length; i++){
            if(newFolders[i].id === id){
                for(let j = 0 ; j < newFolders[i].files.length; j++){
                    const currentFile = newFolders[i].files[j];
                    // return currentFile.language;
                    if(fileId === currentFile.id){
                        newFolders[i].files[j].code = newCode;
                    }
                }
            }
        }
        localStorage.setItem('data', JSON.stringify(newFolders));
        setFolders(newFolders);
    }

    // const [modalPayload, setModalpa]

    const playgroundFeatures = {
        folders,
        // files,
        createNewPlayground,
        createNewFile,
        deleteFolder,
        editFolderTitle,
        saveCode
    }



    useEffect(() => {
        if(!localStorage.getItem('data')){
            localStorage.setItem('data', JSON.stringify(folders))
        }
    },[])


    const uniqId = v4();
    
    const obj = {name: 'divyansh'};
    
    return( 
    <PlaygroundContext.Provider value={ playgroundFeatures }> 
    {children}
    </PlaygroundContext.Provider>
    );

        
};

Kindly tell me why is it not storing anything
Also I am a beginner so pls keep it in your mind

I tried removing Editor Container by putting the code in the index.js itself but it isnt working

New contributor

Divyansh Pathak is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật