我想问一下当我做onclick事件时为什么我的状态没有改变.我刚才搜索过我需要在构造函数中绑定onclick函数,但状态仍未更新.这是我的代码:
import React from 'react';
import Grid from 'react-bootstrap/lib/Grid';
import Row from 'react-bootstrap/lib/Row';
import Col from 'react-bootstrap/lib/Col';
import BoardAddModal from 'components/board/BoardAddModal.jsx';
import style from 'styles/boarditem.css';
class BoardAdd extends React.Component {
constructor(props){
super(props);
this.state = {
boardAddModalShow: false
}
this.openAddBoardModal = this.openAddBoardModal.bind(this);
}
openAddBoardModal(){
this.setState({ boardAddModalShow: true });
// After setting a new state it still return a false value
console.log(this.state.boardAddModalShow);
}
render() {
return (
<Col lg={3}>
<a href="javascript:;" className={style.boardItemAdd} onClick={this.openAddBoardModal}>
<div className={[style.boardItemContainer,style.boardItemGray].join(' ')}>
Create New Board
</div>
</a>
</Col> …Run Code Online (Sandbox Code Playgroud) 我第一次使用React时遇到了一些麻烦.我在我的项目中编写了一个简单的类:
import React, { Component } from 'react';
import axios from 'axios';
import logo from './logo.svg';
import './App.css';
class FigosTest extends React.Component {
state = {
provider: 'unknown',
date: 'sometime'
};
componentDidMount() {
axios.get('x.com/some_api')
.then(res => {
console.log(res.data.provider);
console.log(res.data.date);
this.setState(this.setState(res.data));
console.log("After");
console.log(this.state.provider);
console.log(this.state.date);
});
}
render() {
return this.provider || 'undefined';
}
}
class App extends Component {
render() {
return (
<div className="App">
<header className="App-header">
<img src={logo} className="App-logo" alt="logo" />
<p>
Result: <FigosTest/>
</p>
</header>
</div>
);
}
} …Run Code Online (Sandbox Code Playgroud)