0

I'm working with reactJS and I am trying to use setState so that i can use that state to determine which way the graph needs to rotate but I get an error saying 'setState' is not defined no-undef. Do I need to have a constructor to initialize the state?

Code:

class App extends React.Component {
    render() {
        const {rotate, setRotate} = setState(false)
        return (
            <div className="custom-container">
                <Tree 
                    data={data} 
                    height={600} 
                    width={1000}
                    svgProps={{transform: 'rotate(${rotate ? "90" : "0")'}} 
                />
                <button onClick={() => setRotate(!rotate)}>Rotate</button>
            </div>
        );
    }    
}
6
  • 3
    Although the error message is probably because you haven't imported setState, you can't use setState in class components. Commented Dec 3, 2020 at 21:00
  • 2
    reactjs.org/docs/hooks-intro.html Commented Dec 3, 2020 at 21:03
  • 1
    @GuyIncognito You mean useState right? @kr419 read reactjs.org/docs/state-and-lifecycle.html Commented Dec 3, 2020 at 21:03
  • 1
    In class components as you have, setState is a member of the React.Component class and should be called using the context of that class, via this.setState(). Commented Dec 3, 2020 at 21:04
  • 2
    You're mixing syntaxes really badly, please read through the React documentation thoroughly so you can understand how to use it's different pieces. Commented Dec 3, 2020 at 21:10

3 Answers 3

3

If you are trying to use, "useState" instead of setState, 'useState' is used for having a state in functional components, you will have to convert class component to function component as below and then use 'useState'

export default function App() {

        const [rotate, setRotate] = React.useState(false)
        return (
            <div className="custom-container">
                <Tree 
                    data={data} 
                    height={600} 
                    width={1000}
                    svgProps={{transform: 'rotate(${rotate ? "90" : "0")'}} 
                />
                <button onClick={() => setRotate(!rotate)}>Rotate</button>
            </div>
        );
}
Sign up to request clarification or add additional context in comments.

Comments

2

You could also try this:

class App extends React.Component {
    state = {
      rotate: false
    };

    render() {
        return (
            <div className="custom-container">
                <Tree 
                    data={data} 
                    height={600} 
                    width={1000}
                    svgProps={{transform: 'rotate(${rotate ? "90" : "0")'}} 
                />
                <button onClick={() => this.setState({ rotate: !this.state.rotate})}>Rotate</button>
            </div>
        );
    }    
}

Comments

2

I think you are trying to use useState();

Make sure you import useState at the top first and also use function component instead of the class component :

import React, {useState} from 'react';



function App() {
    const [rotate, setRotate] = useState(false)
    return (
        <div className="custom-container">
            <Tree 
                data={data} 
                height={600} 
                width={1000}
                svgProps={{transform: 'rotate(${rotate ? "90" : "0")'}} 
            />
            <button onClick={() => setRotate(!rotate)}>Rotate</button>
        </div>
    );

Comments

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.