Topic: how to get MDBInput value?

Ominous Vibes priority asked 2 years ago


Title says it all. Is there any getValue event on mdb5? instead of the onChanged event.


Krzysztof Wilk staff answered 2 years ago


Hi!

There are only two ways to get the input value.

Uncontrolled input - the only way to get the value is to use an onChange event.

import React, { useState } from "react";
import { MDBInput } from "mdb-react-ui-kit";

function App() {
  return <MDBInput onChange={(e) => console.log(e.target.value)} />;
}

export default App;

Controlled input - you can use an useState hook to achieve that.

import React, { useState, useEffect } from 'react';
import { MDBInput } from 'mdb-react-ui-kit';

function App() {
  const [inputValue, setInputValue] = useState('');

  useEffect(() => {
    console.log(inputValue);
  }, [inputValue]);

  return (
    <MDBInput value={inputValue} onChange={(e) => setInputValue(e.target.value)} />
  );
}

export default App;

The getValue method is only used in the MDBSelect component due to its complicated structure.

Keep coding!



Please insert min. 20 characters.

FREE CONSULTATION

Hire our experts to build a dedicated project. We'll analyze your business requirements, for free.

Status

Answered

Specification of the issue

  • ForumUser: Priority
  • Premium support: Yes
  • Technology: MDB React
  • MDB Version: MDB5 1.4.0
  • Device: Windows
  • Browser: Opera GX
  • OS: 64
  • Provided sample code: No
  • Provided link: No