3

how can I change the value of an TextInput in react-admin.

My idea was

<TextInput label="..." source="..." value="MY VALUE" />

But that doesn't work. pls help.

1

3 Answers 3

4

You can't do this with the react-admin's TextInput as it is a connected field to the backing react-final-form form and hence you don't control the input.

You should state what is your final aim. I can think of two options:

  1. If you want to have a controlled input you can simply use material-ui's TextField and pass to it your custom value as well as handle the onChange event.

  2. If you don't want to control the value by yourself but alter it in the form state under some condition, you can then use the hook provided by react-final-form:

    const form = useForm();
    form.change("myValue", newValue);
    ...
    <TextInput label="My Value" source="myValue"  />
    
Sign up to request clarification or add additional context in comments.

Comments

3

Since react-adminv4.0.0 library no longer uses react-final-form instead it uses react-hook-form. So you would need to change your relevant code to use it as well.

import {useFormContext} from "react-hook-form";
...
const {setValue} = useFormContext();
setValue("myValue", newValue);

Comments

-1

If the value of your input is null or undefined you can use the initialValue prop to set the value when the page loads.

<TextInput source="mySource" initialValue="MY VALUE" />

See React-Admin Input Docs: https://marmelab.com/react-admin/Inputs.html#common-input-props

If you are looking to dynamically change the value after the page has loaded, I would recommend following Kia's answer to leverage the useForm hook.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.