Reactjs 在我的例子中,如何避免使用2状态变量?

Reactjs 在我的例子中,如何避免使用2状态变量?,reactjs,Reactjs,我正在寻找一种方法来优化我的代码 有没有办法只使用1个状态变量(started)而不是2个(started和now) 这是我的代码(一个非常简单的计时器): var Timer=React.createClass({ getInitialState:函数(){ 返回{ 已开始:新日期().getTime(), 现在:0 } }, 渲染:函数(){ 返回( {this.state.now} 重置 ); }, componentDidMount:函数(){ this.tickInterval=set

我正在寻找一种方法来优化我的代码

有没有办法只使用1个状态变量(started)而不是2个(started和now)

这是我的代码(一个非常简单的计时器):

var Timer=React.createClass({
getInitialState:函数(){
返回{
已开始:新日期().getTime(),
现在:0
}
},
渲染:函数(){
返回(
{this.state.now}
重置
);
},
componentDidMount:函数(){
this.tickInterval=setInterval(this.doTick,1000);
},
componentWillUnmount:函数(){
clearInterval(这个.tickInterval);
},
doTick:function(){
让newDate=newDate().getTime();
让newCounter=Math.round((newDate-this.state.started)/1000);
this.setState({now:newCounter});
},
重置计数器:函数(){
this.setState({start:new Date().getTime(),now:0});
},
});
ReactDOM.render(,document.getElementById('root'));

根据您对精确度的关注程度,您只需在doTick函数中存储类似currentTicks的内容并将其递增1即可。但不能保证每个间隔都是一秒钟

var Timer=React.createClass({
getInitialState:函数(){
返回{
滴答:0,,
}
},
渲染:函数(){
返回(
{this.state.tick}
重置
);
},
componentDidMount:函数(){
this.tickInterval=setInterval(this.doTick,1000);
},
componentWillUnmount:函数(){
clearInterval(这个.tickInterval);
},
doTick:function(){
this.setState({tick:this.state.tick+1});
},
重置计数器:函数(){
这是我的国家({
滴答:0
});
},
});
ReactDOM.render(,document.getElementById('root'))

根据您对精确度的关注程度,您只需在doTick函数中存储类似currentTicks的内容并将其递增1即可。但不能保证每个间隔都是一秒钟

var Timer=React.createClass({
getInitialState:函数(){
返回{
滴答:0,,
}
},
渲染:函数(){
返回(
{this.state.tick}
重置
);
},
componentDidMount:函数(){
this.tickInterval=setInterval(this.doTick,1000);
},
componentWillUnmount:函数(){
clearInterval(这个.tickInterval);
},
doTick:function(){
this.setState({tick:this.state.tick+1});
},
重置计数器:函数(){
这是我的国家({
滴答:0
});
},
});
ReactDOM.render(,document.getElementById('root'))

如果您不关心增加状态,那么这是一种方法

var Timer = React.createClass({

    getInitialState: function () {
        return {
            started: 0,
        }
    },


    render: function () {

        return (
            <div>
                <strong>{this.state.started}</strong>
                &nbsp;
                <button type="button" onClick={this.resetCounter}>Reset</button>
            </div>
        );
    },


    componentDidMount: function () {
        this.tickInterval = setInterval(this.doTick, 1000);
    },


    componentWillUnmount: function () {
        clearInterval(this.tickInterval);
    },

    doTick: function () {
        this.setState({ started:this.state.started+1});
    },
    resetCounter: function () {
        this.setState({ started:0});
    },

    });



    ReactDOM.render(<Timer />, document.getElementById('root'));
var Timer=React.createClass({
getInitialState:函数(){
返回{
开始:0,
}
},
渲染:函数(){
返回(
{this.state.started}
重置
);
},
componentDidMount:函数(){
this.tickInterval=setInterval(this.doTick,1000);
},
componentWillUnmount:函数(){
clearInterval(这个.tickInterval);
},
doTick:function(){
this.setState({started:this.state.started+1});
},
重置计数器:函数(){
this.setState({start:0});
},
});
ReactDOM.render(,document.getElementById('root'));

查看

如果您不关心增加状态,那么这是一种方法

var Timer = React.createClass({

    getInitialState: function () {
        return {
            started: 0,
        }
    },


    render: function () {

        return (
            <div>
                <strong>{this.state.started}</strong>
                &nbsp;
                <button type="button" onClick={this.resetCounter}>Reset</button>
            </div>
        );
    },


    componentDidMount: function () {
        this.tickInterval = setInterval(this.doTick, 1000);
    },


    componentWillUnmount: function () {
        clearInterval(this.tickInterval);
    },

    doTick: function () {
        this.setState({ started:this.state.started+1});
    },
    resetCounter: function () {
        this.setState({ started:0});
    },

    });



    ReactDOM.render(<Timer />, document.getElementById('root'));
var Timer=React.createClass({
getInitialState:函数(){
返回{
开始:0,
}
},
渲染:函数(){
返回(
{this.state.started}
重置
);
},
componentDidMount:函数(){
this.tickInterval=setInterval(this.doTick,1000);
},
componentWillUnmount:函数(){
clearInterval(这个.tickInterval);
},
doTick:function(){
this.setState({started:this.state.started+1});
},
重置计数器:函数(){
this.setState({start:0});
},
});
ReactDOM.render(,document.getElementById('root'));
查看