score:-1

if you will use defaultvalue props you need to convert your value to string first.

example:

const defvalue = 123;

<input defaultvalue={defvalue.tostring()} />

it works it my end.

score:-1

you can use this, it works for me :

const demo = () => {
  const [form] = form.useform();

  react.useeffect(() => {
    form.setfieldsvalue({
      username: 'bamboo',
    });
  }, []);

  return (
    <form form={form}>
      <form.item name="username" rules={[{ required: true }]}>
        <input />
      </form.item>
    </form>
  );
};

score:0

looking at your code, i assume you are not using ant design form.

since it is controlled field, you need to use value prop instead of defaultvalue prop. if you see empty field, your redux initial state for settings may be empty '' string..

set your settings initial value in redux to appropriate value.

score:1

i had this problem when creating data-grid, and couldn't see the defaultvalue for the first input. if you want to use uncontrolled input you can use your custom input which is controlled and set a callback function to get results like from uncontrolled input. in my case, i used onblur action to get changed value.

const cusomeinput = ({ defaultvalue, oninputblur }) => {
  const [inputstate, setinputstate] = usestate("");
  useeffect(() => {
    setinputstate(defaultvalue);
  }, [defaultvalue]);
  return <input value={inputstate} onchange={(e) => setinputstate(e.target.value)} onblur={oninputblur} />;
};

score:1

you can either achieve you target by putting a ref to the form

const formref = react.useref(null)

useeffect(()=>{
formref?.current?.setfieldsvalue({
  name: fieldstopopulate?.name,
  email: fieldstopopulate?.email,
...
})
},[fieldstopopulate])

return(
<form onfinish={handlefinish ref={formref}>
<form.item name="name" rules={[...]}>
<input {...props} />
<form.item name="email" rules={[...]}>
<input {...props} />
</form.item>
...
</form>
)

score:3

the correct thing is that you use fields. your code should look like.

<form
  name="form_name"
  fields={[
    {
      name: ["fieldname"],
      value: settingsredux.background_image,
    },
  ]}
>
  <form.item name="fieldname">
    <input
      onchange={(e) =>
        dispatch({
          type: background_image,
          payload: e.target.value,
        })
      }
    />
  </form.item>
</form>;

you can see more examples in the documentation https://ant.design/components/form/

score:7

you may set initialvalues at form component as object.

<form
  ...
  initialvalues={{
    ["name"]: settingsredux.background_image 
  }}
>
 ...

<input 
  name="name"
  onchange={e => dispatch({
    type: background_image, 
    payload: e.target.value
  })}
/>

checkout here:https://ant.design/components/form/

score:8

you may want to use value prop instead of defaultvalue. the default value is used when you want to set an initial value in an uncontrolled input. currently, you have made your component controlled so you may have to use value prop.

read more about this here: https://reactjs.org/docs/forms.html#controlled-components


Related Query

More Query from same tag