小编JJN*_*L77的帖子

类型错误:未定义不是对象(评估“this.setState”)

我一直收到这个错误,我不知道为什么,因为我尝试过的一切都不起作用。有谁知道为什么这不起作用以及它如何工作?

我在这里未定义:

this.setState({isAuthenticated: true})
Run Code Online (Sandbox Code Playgroud)

这是我的代码:

class Login extends Component{

        constructor(props){
            super(props);

            this.state ={
                email: '',
                password: '',
                isAuthenticated: false
            };

            function login(username, email){
                sessionStorage.setItem('loginSessionUsername', username);
                sessionStorage.setItem('loginSessionEmail', email);
                this.setState({isAuthenticated: true})
            }
        }

        render(){
            const isAuthenticated = this.state.isAuthenticated;
            if(isAuthenticated){
                return(
                    <div>
                        <Servicedesk />     
                    </div>
                )
            }
            return(
                <div id='Login' className='setVisible'>
                    <div>
                        <label>Emailadres</label>
                        <input type='text' placeholder='je email' onChange={ev => this.setState({email: ev.target.value})}/>
                        <label>Wachtwoord</label>
                        <input type='password' placeholder='je wachtwoord' onChange={ev => this.setState({password: ev.target.value})}/>
                        <br />
                        <button onClick={(event => this.handleClick(event))}>Submit</button>
                    </div>
                </div>
            )
        }
    }

    export default …
Run Code Online (Sandbox Code Playgroud)

javascript reactjs

6
推荐指数
1
解决办法
1万
查看次数

从反应本机数组中的对象获取密钥

constructor(props) {
    super(props);
    this.state = ({
        childData: [],
        test: []
    });
}

componentDidMount() {
    let userId = firebase.auth().currentUser.uid;
    firebase.database().ref('poolog/' + userId).on('value', (snapshot) => {
       let test = [];
       snapshot.forEach((childSnapshot) => {
          let childKey = childSnapshot.key;
          test.push(snapshot.val());
        });
        this.setState({childData: test});
        // console.log(this.state.childData);
    });
Run Code Online (Sandbox Code Playgroud)

我在React的返回方法中使用了这个函数。

 {this.state.childData.map((item, key) =>
     <View key={key}>
        {console.log(Object.values(item) + 'test')}
        <Text>{Object.keys(item)}</Text>
        {Object.values(item).map((value, index) =>
          <View></View>
        )}
     </View>
  )}
Run Code Online (Sandbox Code Playgroud)

我有一个问题,我只想要一个特定的值,Object.values(item)给出所有值,当我使用Object.values(item[key])Object.values(item[0])我得到:

TypeError: undefined is not an object (evaluating 'Object.keys(item[0]') 或 TypeError: undefined is not an …

javascript reactjs react-native

4
推荐指数
1
解决办法
2万
查看次数

标签 统计

javascript ×2

reactjs ×2

react-native ×1