In my REACT app, I have a Context that I have set up inside this component like so:…
- User
In my REACT app, I have a Context that I have set up inside this component like so:
const { source } = useContext(SourceContext);The source is a string, and right now it could be 'Database' or 'Local', to distinguish whether we are browsing images stored in the FastAPI endpoints database, or from the local file system. I have a component I am going to add to, but these buttons should only appear is the source is 'Local', and not for 'Database', as we will be adding images to the database using it. It is in a TabPane like this:
<TabPane tab="Tools" key="tools"> <Flex> <Button onClick={startDBCleanup}>Run DB Cleanup</Button> </Flex> </TabPane>We also have this:
const selectedImages = images.filter((image) => selectedImageIds.has(image.id) );And each item will have .id, this id is the local file path, we want to use these FastAPI endpoints:
@router.post("/images/add-folder/") def addfolder_image_endpoint(path: str, background_tasks: BackgroundTasks): try: # Add background task to run the cleanup after sending the response background_tasks.add_task(add_folder_images_task, path) return {"message": f"Starting image adding task on {path}"} except Exception as e: raise HTTPException(status_code=500, detail=f"Unable to initiate cleanup task: {str(e)}") @router.post("/images/add-file/", response_model=schemas.Image) def addfile_image_endpoint(image_file_path: str, db: Session = Depends(get_db)): return crud.add_image(db=db, image_file_path=image_file_path)What I want is two buttons, one to add the current path and 'add-folder', and the other is to process all the selectedImages and send them through 'add-file'.
One thing to take care of first though, I have this state:
const [currentPath, setCurrentPath] = useState(basePath || "");Stored deep in another component(LocalFileBrowser/index.jsx), I need to get access to it in the parent component(ImagesPage/index.jsx), I figured without messing with the existing state, I would use a REACT useRef in the ImagesPage like this:
const [path] = useRef("");And set propagate that down to LocalFileBrowser and the current component I am working on: ControlPanel/index.jsx. However, I am not sure how to set the reference, can I just assign it like this(In LocalFileBrowser, I have passed path into it as a prop):
useEffect(() => { fetchDirectoryContents(currentPath); fetchDirectoryImages(currentPath); path = currentPath; }, [currentPath, fetchDirectoryContents, fetchDirectoryImages]);What is the proper way I should go about getting the currentPath from LocalFileBrowser across to ControlPanel?
- Assistant
- User
- Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.