I am trying to make a recursive class component to check if an input is a palindrome or not. but when I am trying to call the palindrome function inside of this function its starts invoking indefinitely. using vanilla js this method worked just fine. what I am doing wrong here? and how to change the output when the input field is empty.
import React, { Component } from "react";
import "./App.css";
export default class App extends Component {
state = { data: "", palindrome: "" };
inputHandeler = (e) => {
this.setState({ data: e.target.value });
};
palindrome = () => {
if (this.state.data.length === 1 || this.state.data.length === 0) {
this.setState({ palindrome: "this is palindrome" });
} else if (
// input abba console.log("indefinitely ")
this.state.data[0] === this.state.data[this.state.data.length - 1]
) {
this.palindrome(this.state.data.slice(1, this.state.data.length - 1));
} else {
this.setState({ palindrome: "this is not palindrome" });
}
};
render() {
return (
<div className="container">
<h1> Palindrome Checking App</h1>
<div className="app">
<div className="inputfield">
<label name=" palindrome ">
Input text to check if it is a palindrome:
</label>
<input id="palindrome " type="text" onChange={this.inputHandeler} />
<button onClick={this.palindrome}>Check</button>
</div>
<div className="output">{this.state.palindrome}</div>
</div>
</div>
);
}
}
this.palindrome()takes no arguments, so I'm not sure what you expect this line to accomplish:this.palindrome(this.state.data.slice(1, this.state.data.length - 1));