Alfonso Rodriguez's avatar

Unable to setData if the data has a json attribute

Hello friends, I have a problem with a json, has anyone gone through something similar? I use React with inertia.

I have data that I want to save in json format, something like...

const { data, setData, post, processing, errors, reset } = useForm({
        contact_one: {
            name: "",
            email: "",
            phone_number: "",
            extension: "",
            position: "",
        }
});
useEffect(() => {
        return () => {
            reset( "contact_one");
        };
    }, []);
const onHandleChange = (event: React.ChangeEvent<HTMLInputElement>) => {
        setData("contact_one");
    };

The information that I have in data is passed as props to a child component where the entries are according to the json, but filling these entries does not make the change in the value of what exists in data.

<InputBase
    label="Contact name"
    value={data.contact_one.name}
    name="contact_one.name"
    id="contact_one.name"
    placeholder="contact_one_name"
    handleChange={onHandleChange}
    />

Am I doing something wrong or is there a better way to do this?

I should mention that I'm moving from vue.js to react.js, sorry if something doesn't look right and also because of my English I'm learning.

0 likes
2 replies
MohamedTammam's avatar
Level 51

To handle the change in the input is like

<InputBase
    label="Contact name"
    value={data.contact_one.name}
    name="contact_one.name"
    id="contact_one.name"
    placeholder="contact_one_name"
    onChange={e => setData('contact_one', {...contact_one, name: e.target.value})}
/>
1 like

Please or to participate in this conversation.