mcfly soft
mcfly soft

Reputation: 11645

Using useRef hook with React-Admin

I try to create a reference to a React-Admin element using the useRef hook, but I do not succeed.

Same doing with native react.js works fine using a simple <input> field, but with a react-admin component the reference is null.

Anyone knows how to reference an element in react-admin ?

const myForm = (props) => {

    const refContainer = useRef(null);

    const myClick = () => {
        console.log('clicked !', refContainer); // refContainer is null !!!
        refContainer.current.value = '1';
    };
    return <Edit {...props}>
        <SimpleForm>
        <TextInput source='myValue' ref={refContainer} value='0'/>
        <input type="button" value="change" onClick={myClick}/></SimpleForm>
    </Edit>;
};

Upvotes: 0

Views: 1098

Answers (1)

MaxAlex
MaxAlex

Reputation: 3319

Try to use instead ref: inputRef https://material-ui.com/ru/api/input/

<TextField inputRef={refContainer} />

Upvotes: 1

Related Questions