How can I acess value from textfield in another module in reactJS

Viewed 218

I want to have access to the value of a textField in another module in reactjs. The module that wants to have access does not import the whole textfield but only needs access to the value. What is the best way to access the value variable in another module in reactjs?

Here is my functional textField component:

export default function textField(props) {

  const [value, setValue] = React.useState("");

  const handleChange = (event) => {
    setValue(value);
  };

  return (
    <div>
      <TextField
        id="outlined-multiline-static"
        label="Frage"
        multiline
        onClick={handleClickOpen}
        rows={4}
        value={value}
        placeholder="hello"
        variant="outlined"
        style={{
          backgroundColor: "white",
        }}
      />
    </div>
  );
}
4 Answers

You can send onTextFieldChange function as props whenever textField's value changes you can pass a value to onTextFieldChange function and you can use it in the parent component.

There is an alternate way, Redux

You should try to use redux for the shared state between components which are either not related directly(i.e. sibling components or have a lengthy hierarchy). For small applications, redux is overkilled so should be avoided.

The most likely option that comes to mind here is the concept of lifting state, in which the nearest ancestor component has some means by which it also keeps track of the state, and the passes it into the sibling that needs to track it. You could make this an optional feature of your module by allowing a onChangeCallback prop that is called on each change; this prop could then be passed a setSharedState hook that would set the state on the ancestor:

const ParentComponent = () => {
    const [textfieldVal, setTextfieldVal] = useState();
    return (
        <TextField onChangeCallback={setTextFieldVal} />
    );
}

And you update your module to something like:

export default function textField(props) {

  const [value, setValue] = React.useState("");
  const {onChangeCallback} = props;

  const handleChange = (event) => {
    setValue(value);
    if (typeof onChangeCallback === 'function') {
      onChangeCallback(event); // or value, I'm not sure which you should be using here, this might be incorrect
    }
  };

  return (
    <div>
      <TextField
        id="outlined-multiline-static"
        label="Frage"
        multiline
        onClick={handleClickOpen}
        rows={4}
        value={value}
        placeholder="hello"
        variant="outlined"
        style={{
          backgroundColor: "white",
        }}
      />
    </div>
  );
}

This is just a rough example. Other options for passing around state freely would be using Redux or the Context API, but the former might be overkill for this one case and the latter probably not a great fit for a specific, single-use datapoint.

there are may option but the proper option in pass as props

const modle1= () => {
    const [textfieldVal, setTextfieldVal] = useState();
    return (
        <TextField onChangeCallback={setTextFieldVal} />
    );
}

export default function textField(props) {

  const [value, setValue] = React.useState("");
  const {onChangeCallback} = props;

  const handleChange = (event) => {
    setValue(value);
    if (typeof onChangeCallback === 'function') {
      onChangeCallback(event); // or value, I'm not sure which you should be using here, this might be incorrect
    }
  };

  return (
    <div>
      <TextField
        id="outlined-multiline-static"
        label="Frage"
        multiline
        onClick={handleClickOpen}
        rows={4}
        value={value}
        placeholder="hello"
        variant="outlined"
        style={{
          backgroundColor: "white",
        }}
      />
    </div>
  );
}

or you can use create context and use context and provide the parent to provide and the child as a consumer but the best option pass as a prop

I would recommend you use the parent-child nature of react in order to handle this. Since I'm not sure the relationship between the other module and this module, I'll provide a rough skeleton.

In a parent component:

export default function ParentComponent(){
  const [status, updateStatus] = useState("")
  
  return(
    <TextView updateParent={updateStatus}>
    
    </TextView>
  )
}

Then in your child component:

const handleChange = (event) => {
    setValue(value);
    props.updateParent(value);
  };

If they are siblings, I would use a parent component and then pass the state down to the sibling. Otherwise, as appropriate, use this parent child relationship to pass and update state.

HTH

Related