3

Working on a tutorial atm that involves react material-ui tables that also has a search input textfield. What I am trying to add to it, is a button that will reset the table report but also clear the search input textfield.

It is the clearing of the search textfield that I am having trouble with.

They are using this code as a separate component library called Controls.input:

import React from 'react' import { TextField } from '@material-ui/core';

export default function Input(props) {

    const { name, label, value,error=null, onChange, ...other } = props;
    return (
        <TextField
            variant="outlined"
            label={label}
            name={name}
            value={value}
            onChange={onChange}
            {...other}
            {...(error && {error:true,helperText:error})}
        />
    )
}

The main search code is as follows where I have also added a button

            <Controls.Input
                id="name"
                label="Search Name"
                className={classes.searchInput}
                InputProps={{
                    startAdornment: (<InputAdornment position="start">
                        <Search />
                    </InputAdornment>)
                }}
                onChange={handleSearch}
            />
            <Button
                onClick={handleClear}
                className="materialBtn"
            >
                Clear
            </Button>

At this point, I am not sure how to reference/target the search input field as part of the handleClear function, in-order to clear it's contents?

const handleClear = () => {
    ????
}    

Do I need to use useState()?

3
  • You will have to clear the value of text input. You can useState to keep the value and clear it in handleClear function . Commented Jun 12, 2021 at 10:07
  • @BhaskarJoshi - sorry, how can I access the value of the text input inorder to use useState?
    – ArthurJ
    Commented Jun 12, 2021 at 10:18
  • stackoverflow.com/questions/69257278/…
    – Ievgen
    Commented Jun 30, 2022 at 22:14

1 Answer 1

5

You are right with having to put the value into state. Based on what you have supplied it seems that your state needs to be in your parent component. So something like this should work

import { useState } from 'react'

const ParentComponent = () => {
    const [value, setValue] = useState('')

    const handleClear = () => {
        setValue('')
    }

    const handleSearch = (event) => {
        setValue(event.target.value)
    }

    return (
        <>
            <Controls.Input
                id="name"
                label="Search Name"
                className={classes.searchInput}
                value={value}
                onChange={handleSearch}
                InputProps={{
                    startAdornment: (
                        <InputAdornment position="start">
                            <Search />
                        </InputAdornment>
                    ),
                }}
                
            />
            <Button onClick={handleClear} className="materialBtn">
                Clear
            </Button>
        </>
    )
}

2
  • Seem to have an issue with the value={value} as this is not allowing me to enter any value into my search input field, just stays null. Any ideas pls?
    – ArthurJ
    Commented Jun 12, 2021 at 10:35
  • 1
    I did a small edit on the answer. But the reason you cant enter anything is because now the value is coming from the value state. So what you need to do is make sure that the onChange is working correctly. I would recommend removing the value={value} line on the input component first and put a console.log in the handleSearch function to make sure that a value is being changed Commented Jun 12, 2021 at 10:39

Not the answer you're looking for? Browse other questions tagged or ask your own question.