0

I am trying to pass the data to parent component using handleclick but its functioning fine.

But it sends data to the parent like if I typed option it sends at parent optio missing the last charactor.

Below is the call to the child component where onClickTitles I am fetching the data

  <CreateEvent onClickTitles={this.handlerPollsValue} initialValues={this.state.init} />

In the CreateEvent component here, this is my handleInputChange function

const [zValues, setValues] = useState('');

const handleInputChange = e => {
    const { name, value } = e.target;
    setValues({
      ...zValues,
      [name]: value,
    });
// here setting the values
        onClickTitles(pValues);
      };

Where I am setting the onClickTitles values where pValues is the value I am setting using setValues

const pValues = [
    {
      title: zValues.option1,
      index: 0,
    },
    {
      title: zValues.option2,
      index: 1,
    },
  ];

Everything works well but not getting the last charactor of what I type in input. (I am using multiple <input> compoennt for data inputs)

Can anyone help me here to resolve this?

  • You probably try to access a state (`this.state.value` or `value`) after either `this.setState({value: newValue})` or `setValue(newValue)`. Calling either of these two functions does not immediately updates the state, but merely queues the update. See [useState set method not reflecting change immediately](/q/54069253/3982562) and [Why calling react setState method doesn't mutate the state immediately?](/q/30782948/3982562) You should use `onClickTitles(newValue)` instead of `onClickTitles(value)` – 3limin4t0r Jul 26 '21 at 14:30
  • Without more context it is most likely not possible to accurately give an solution to your problem. What is `zValues`? When is `pValues` set to the provided structure? Could you provide more context regarding the component? – 3limin4t0r Jul 26 '21 at 14:38
  • @3limin4t0r Can you help me with this? I am very much stuck with this – Maria Andre Jul 26 '21 at 14:44
  • @3limin4t0r here's full component code https://codesandbox.io/s/fervent-goldberg-upbzl?file=/CreatePollComponent.js – Maria Andre Jul 26 '21 at 14:53
  • `const [zValues, setValues] = useState("");` seems incorrect if you want to assign properties via `setValues`. Can you try `const [zValues, setValues] = useState({});`? – edemaine Jul 26 '21 at 17:41

0 Answers0