Reputation: 1231
When I write the following code
import React,{Component} from 'react';
import ReactDOM from 'react-dom';
import './index.css';
class Date extends React.Component{
dataToString = (d) =>{
return [
d.getFullYear(),
d.getMonth + 1,
d.getDate()
].join('-')
}
Error as shown below,thanks everyone。
Uncaught TypeError: d.getFullYear is not a function
at Date._this.dataToString (index.js:10)
at Date.render (index.js:18)
at ReactCompositeComponent.js:796
at measureLifeCyclePerf (ReactCompositeComponent.js:75)
at
Online, thank you
Upvotes: 0
Views: 60
Reputation: 104529
This is not the right way writing React Component
.
Issue is: Whenever we use any React.Component
, we have to define a render
method. That is the starting
point for that component
, means this is the place from where React starts the UI creation
, and you didn't define the render
method.
If you just want to write a file that contains only some generic methods
, create it like this:
module.exports{
dataToString : function(d){
return [
d.getFullYear(),
d.getMonth() + 1,
d.getDate()
].join('-');
},
dateOnly: function(d){
/*write your logic here*/
return d;
}
}
You can use these methods by importing
this file anywhere in the project.
Or if you want to write it like it as a component
, then define the render
method also, like this:
class DateClass extends React.Component{
dataToString(d) {
return [
d.getFullYear(),
d.getMonth() + 1,
d.getDate()
].join('-')
}
render(){
return <div>{this.dataToString(new Date())}</div>
}
}
Let me know if you need any help.
Upvotes: 1