1

I try get a value of my option/select for use it in states in future, but when I try get element by ID, i got error "Cannot read property 'value' of null"

import React from "react";

function Inputs() {
  const Period = document.getElementById("Period");

  console.log(Period);

  const inputHandler = (inputName) => {
    console.log(inputName.value);
  };

  return (
    <div>
      <label>Period:</label>
      <select id="Period" onChange={() => inputHandler(Period)}>
        <option>Last 12 month</option>
        <option>Last 6 month</option>
        <option>Last month</option>
      </select>
    </div>
  );
}

export default Inputs;

Link to the SandBox

2
  • 1
    Try to do it in useEffect, because at initial the select tag not initialized Commented Nov 30, 2020 at 19:38
  • The reason why Period is null is because when the component is mounted, there is no DOM painted already so the selector is not able to find an non-exist element Checkout this solution codesandbox.io/s/hungry-snow-4ylgb?file=/src/App.js Commented Nov 30, 2020 at 19:52

4 Answers 4

1

Check the solution bellow:

import React from "react";

function RechartsInputs() {
  const Period = document.getElementById("Period");

  console.log(Period);

  const inputHandler = (e) => {
    console.log(e.target.value);
  };

  return (
    <div>
      <label>Period:</label>
      <select id="Period" onChange={e => inputHandler(e)}>
        <option>Last 12 month</option>
        <option>Last 9 month</option>
        <option>Last 6 month</option>
        <option>Last 3 month</option>
        <option>Last month</option>
      </select>
    </div>
  );
}

export default RechartsInputs;

hope it helps ;D

Sign up to request clarification or add additional context in comments.

Comments

1

Your logic is wrong. Follow with this. Use useEffect and useState.

import React, { useEffect, useState } from "react";

function RechartsInputs() {
  const [val, setVal] = useState(null);

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

  const inputHandler = (event) => {
    setVal(event.target.value);
  };

  return (
    <div>
      <label>Period:</label>
      <select id="Period" onChange={inputHandler}>
        <option>Last 12 month</option>
        <option>Last 9 month</option>
        <option>Last 6 month</option>
        <option>Last 3 month</option>
        <option>Last month</option>
      </select>
    </div>
  );
}

export default RechartsInputs;

1 Comment

Your solution are good and work, but I'm should not use States at this component, because i'm pass this values on neighboring component through the parent component
1

It's not recommended to use document.getElementById with React this way.

You need to understand that you are using jsx which is a templating engine that reads all the tags that your component is returning and compiles it to JavaScript in order to generate the HTML. So what you are doing is trying to query the element before it's rendered and thats why you get null.

You could use a lifecycle callback like componentDidMount or a hook like useEffect in order to get notified when the HTML was rendered and then use document.getElementById but it's not recommended to access elements like this.

In order to get a reference to the element you can use refs even though it's better to create a handler for the onChange event and get the value from the event target.

Comments

0

You are trying to access the input before the render at that time it will be obviously null

Use the event.target to access the input

import React from "react";

function Inputs() {

  const inputHandler = (e) => {
    console.log(e.target.value);
  };

  return (
    <div>
      <label>Period:</label>
      <select id="Period" onChange={(e) => inputHandler(e)}>
        <option>Last 12 month</option>
        <option>Last 6 month</option>
        <option>Last month</option>
      </select>
    </div>
  );
}

export default Inputs;

2 Comments

Thank you, but your try run your code ? It doesn't work
@Miltosh t h I don't have system to run react atm. Does any other code work?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.