0

I have a functional component but I need in class component. So I tried to change this but had some error but I can't find what I'm missing.

import React, { useState } from 'react';
import DateTimeRangePicker from '@wojtekmaj/react-datetimerange-picker';

function App() {
  const [value, onChange] = useState([new Date(), new Date()]);

  return (
    <div>
      <DateTimeRangePicker
        onChange={onChange}
        value={value}
      />
    </div>
  );
}
export default App

I tried this but it's not working:

import React, { Component } from 'react'
import DateTimeRangePicker from '@wojtekmaj/react-datetimerange-picker';

export default class App extends Component {
  constructor(props){
    super()
    this.state = {
      value:new Date()
    }
  }
  render() {
    return (
      <div>
        <DateTimeRangePicker
          onChange={() => this.setState({value:new Date()})}
          value={this.state.value}
        />    
      </div>
    )
  }
}
5

1 Answer 1

1

As explained in npm page, onChange function returns a value. So you could change class component like this:

import React, { Component } from 'react'
import DateTimeRangePicker from '@wojtekmaj/react-datetimerange-picker';

export default class App extends Component {
  constructor(props){
    super(props)
    this.state = {
      value:[new Date(), new Date()],
    }
  }
  render() {
    return (
      <div>
        <DateTimeRangePicker
          onChange={(value) => this.setState({value:value})}
          value={this.state.value}
        />    
      </div>
    )
  }
}
Sign up to request clarification or add additional context in comments.

2 Comments

Actually The value is an array , But I used single value that's why it didn't worked
@arunlazer so should be something like alue:[new Date(), new Date()], correct?

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.