I wrote a simple jira plugin which adds links to revision numbers in a custom field, however when I enter edit mode, it clears the field even if there was a previous value in it.Code looks like this:
Hi @Ádám Smiló
You are not initializing the field with its current value, so it shows an empty field when editing and submits an empty string unless the user manually re-enters it.
In Edit.jsx, you're not using view.getContext() to load the current field value. As a result, the field resets to "" in edit mode and clears the value on submit.
view.getContext()
""
It should be something like the below.
import React, { useState, useEffect, useCallback } from "react";import ForgeReconciler, { Textfield } from "@forge/react";import { CustomFieldEdit } from "@forge/react/jira";import { view } from "@forge/bridge";const Edit = () => {const [value, setValue] = useState("");// Load the current field value when the edit view is openeduseEffect(() => {view.getContext().then((context) => {if (context.extension.fieldValue != null) {setValue(context.extension.fieldValue);}});}, []);const onSubmit = useCallback(() => {view.submit(value);}, [value]);return (<CustomFieldEdit onSubmit={onSubmit}><Textfieldvalue={value}onChange={(e) => {setValue(e.target.value);}}/></CustomFieldEdit>);};ForgeReconciler.render(<React.StrictMode><Edit /></React.StrictMode>);
I added this hook to the code, it gets the context and correctly, I tested it with console.log commands, but it can't set the value for some reason. Do you know how can I debug this?
Tracking the logs is how you debug your code.
You need to add logs to identify where the problem lies and fix it accordingly.
I added these console logs to the code:
Hmm, I'm not sure but it is worth trying to only render when value is defined.
Could you try the below updated code?
import React, { useState, useEffect, useCallback } from "react";import ForgeReconciler, { Textfield } from "@forge/react";import { CustomFieldEdit } from "@forge/react/jira";import { view } from "@forge/bridge";const Edit = () => {const [value, setValue] = useState(undefined); // Start as undefineduseEffect(() => { view.getContext().then((context) => { console.log(context.extension.fieldValue); const existingValue = context.extension.fieldValue || ""; setValue(existingValue); console.log("Set value ran"); });}, []);const onSubmit = useCallback(() => { view.submit(value || "");}, [value]);return ( <CustomFieldEdit onSubmit={onSubmit}> {value !== undefined && ( <Textfield value={value} onChange={(e) => setValue(e.target.value)} /> )}</CustomFieldEdit>);};ForgeReconciler.render(<React.StrictMode><Edit /></React.StrictMode>);
It looks like you're new here. Sign in or register to get started.