React jquery ajax不会更新状态

时间:2017-10-03 13:46:33

标签: javascript jquery reactjs

我在构造函数中设置初始状态并启动ajax调用并成功setState新日期。我渲染{this.state.anything}并且它不会改变状态。我试过这个:

export class Iconpack extends React.Component {
    constructor (props) {
        super(props)
        this.state = {
            first_price_string: '',
            second_price_string: ''
        }
    }

    doSomthingforgodsake () {
        $.ajax({
            type: 'POST',
            url: '...',
            data: '...',
            contentType: 'application/json',
            dataType: 'json',
            success: function (data) {
                this.setState({

                    first_price_string: data.data[0].open_price,
                    second_price_string: data.data[0].high_price
                })
            }.bind(this)
        })
    }

    componentDidMount () {
        this.doSomthingforgodsake()
    }

    render () {
        const firstpricethatthereis = this.state.first_price_string
        return (
            <div>
                <div className='b1-icon' >
                    <p className='b1-price b1-first' >
                        { firstpricethatthereis }
                    </p>
                </div>
            </div>
        )
    }
}

0 个答案:

没有答案