React Hook “useState” is called in function “app” which is neither a React function component or a custom React Hook function

前端 未结 29 874
花落未央
花落未央 2020-11-30 00:07

I\'m trying to use react hooks for a simple problem

const [personState,setPersonState] = useState({ DefinedObject });

with following depend

相关标签:
29条回答
  • 2020-11-30 00:31

    As far as I know a linter is included into the this package. And it requires you componend should begin from Capital character. Please check it.

    However as for me it's sad.

    0 讨论(0)
  • 2020-11-30 00:32

    Whenever working with a React functional component, always keep the first letter of the name of the component in Uppercase in order to avoid these React Hooks errors.

    In your case, you have named the component app, which should be changed to App, as I said above, to avoid the React Hook error.

    0 讨论(0)
  • 2020-11-30 00:33

    Do you have the right import ?

    import React, { useState } from 'react';
    
    0 讨论(0)
  • 2020-11-30 00:36

    Use Capital letter for defining functional component name/ React hooks custom components. "const 'app' should be const 'App'.

    App.js

    import React, { useState } from 'react';
    import { Component } from 'react';
    import logo from './logo.svg';
    import './App.css';
    import Person from './Person/Person';
    
    const App = props => {
      const [personState, setPersonState] = useState({
        persons : [
              {name: 'a', age: '1'},
              {name: 'b', age: '2'},
              {name: 'c', age: '3'}
        ]
      });
    
        return (
          <div>
         <Person name = {personState.persons[0].name} age={personState.persons[0].age}> First </Person>
         <Person name = {personState.persons[1].name} age={personState.persons[1].age}> Second </Person>
         <Person name = {personState.persons[2].name} age={personState.persons[2].age}> Third </Person>    
        );
    };
    export default App;
    

    Person.js

    import React from 'react';
    
    const person = (props) => {
    return (
            <div>
    <p> My name is {props.name} and my age is {props.age}</p>
    <p> My name is {props.name} and my age is {props.age} and {props.children}</p>
    <p>{props.children}</p>
            </div>
    )
    };
    

    [ReactHooks] [useState] [ReactJs]

    0 讨论(0)
  • 2020-11-30 00:37
            import React, { useState } from "react"
    
        const inputTextValue = ({ initialValue }) => {
            const [value, setValue] = useState(initialValue);
            return {
                value,
                onChange: (e) => { setValue(e.target.value) }
            };
        };
    
        export default () => {
            const textValue = inputTextValue("");
            return (<>
                <input {...textValue} />
            </>
            );
        }
    
    /*"Solution I Tired Changed Name of Funtion in Captial "*/
    
        import React, { useState } from "react"
    
    const InputTextValue = ({ initialValue }) => {
        const [value, setValue] = useState(initialValue);
        return {
            value,
            onChange: (e) => { setValue(e.target.value) }
        };
    };
    
    export default () => {
        const textValue = InputTextValue("");
        return (<>
            <input {...textValue} />
        </>
        );
    }
    
    0 讨论(0)
  • 2020-11-30 00:38

    Use const App instead of const app

    0 讨论(0)
提交回复
热议问题