Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/react-native/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 使用循环更改零部件特征(样式)_Javascript_React Native - Fatal编程技术网

Javascript 使用循环更改零部件特征(样式)

Javascript 使用循环更改零部件特征(样式),javascript,react-native,Javascript,React Native,是否使用无限循环遍历每个组件(例如:按钮/标签)特征?在我的应用程序中,我在一个特定的屏幕上有24个按钮,我想一直逐个更改每个按钮的颜色。我想一直一个一个地改变每个按钮的颜色。我尝试了componentdidmount和componentwillmount,但只发生过一次。当我转到另一个屏幕并返回时,循环不会启动。如果您希望在某个时间间隔内执行此操作,请跟踪处于您状态的选定项目,例如: // In your constructor (since you mentioned `componentD

是否使用无限循环遍历每个组件(例如:按钮/标签)特征?在我的应用程序中,我在一个特定的屏幕上有24个按钮,我想一直逐个更改每个按钮的颜色。我想一直一个一个地改变每个按钮的颜色。我尝试了
componentdidmount
componentwillmount
,但只发生过一次。当我转到另一个屏幕并返回时,循环不会启动。

如果您希望在某个时间间隔内执行此操作,请跟踪处于您状态的选定项目,例如:

// In your constructor (since you mentioned `componentDidMount`, I know you're using classes)
this.state = {
    selectedControl: 0,
    // ...your other state
};
componentDidMount
中,启动间隔计时器:

componentDidMount() {
    this.timerHandle = setInterval(() => {
        this.setState(({selectedControl, controls}) =>
           ({selectedControl: (selectedControl + 1) % controls.length})
        );
    }, 2000); // 2000ms = two seconds
}
渲染控件时,高亮显示选定控件:

render() {
    const {selectedControl, controls} = this.state;
    return (
        <div>
            {controls.map((control, index) => (
                <input key={index} type="button" value={control} className={index === selectedControl ? "highlighted" : undefined} />
            ))}
        </div>
    );
}
。突出显示{
字体大小:粗体;
}


是什么触发了每个循环迭代?时间用户操作?@T.J.Crowder我想在用户只进入特定屏幕时触发循环,但是什么触发了每次循环迭代?我需要自动触发每次迭代请努力写一个好问题以避免它被关闭,如果你的问题非常清楚,你更有可能得到有用的答案。请回顾如何提问:这正是我一直在寻找的东西,谢谢!