te')); return $arr; } /* 遍历用户所有主题 * @param $uid 用户ID * @param int $page 页数 * @param int $pagesize 每页记录条数 * @param bool $desc 排序方式 TRUE降序 FALSE升序 * @param string $key 返回的数组用那一列的值作为 key * @param array $col 查询哪些列 */ function thread_tid_find_by_uid($uid, $page = 1, $pagesize = 1000, $desc = TRUE, $key = 'tid', $col = array()) { if (empty($uid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('uid' => $uid), array('tid' => $orderby), $page, $pagesize, $key, $col); return $arr; } // 遍历栏目下tid 支持数组 $fid = array(1,2,3) function thread_tid_find_by_fid($fid, $page = 1, $pagesize = 1000, $desc = TRUE) { if (empty($fid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('fid' => $fid), array('tid' => $orderby), $page, $pagesize, 'tid', array('tid', 'verify_date')); return $arr; } function thread_tid_delete($tid) { if (empty($tid)) return FALSE; $r = thread_tid__delete(array('tid' => $tid)); return $r; } function thread_tid_count() { $n = thread_tid__count(); return $n; } // 统计用户主题数 大数量下严谨使用非主键统计 function thread_uid_count($uid) { $n = thread_tid__count(array('uid' => $uid)); return $n; } // 统计栏目主题数 大数量下严谨使用非主键统计 function thread_fid_count($fid) { $n = thread_tid__count(array('fid' => $fid)); return $n; } ?>javascript - How to change button on pressed color in multiple buttons React Native - Stack Overflow
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to change button on pressed color in multiple buttons React Native - Stack Overflow

programmeradmin3浏览0评论

I have 3 buttons in my react native app. When user clicks button 1 I need to change it color to orange. But other buttons should have the default color (grey). If user clicks the button 3 next time, color should change to orange ,but that 1st button color should reset to default. I'm totally new to react native and this is what I tried. But it applies for all buttons. I know if I can have multiple states with unique Id , it can be done. But I don't know the method.

<Text style={ styles.switchButtonsTitle }>Choose Type of User</Text>
<TouchableOpacity onPress={(userType) =>
    this.selectionOnPress("BASIC")} >
    <Text style={_style}>
        <Text style={styles.switchButtonsText}>BASIC</Text>
    </Text>
</TouchableOpacity>


<TouchableOpacity onPress={(userType) =>
    this.selectionOnPress("INTERMEDIATE")}>
    <Text style={_style}>
        <Text style={styles.switchButtonsText}>INTERMEDIATE</Text>
    </Text>
</TouchableOpacity>

<TouchableOpacity onPress={(userType) =>
    this.selectionOnPress("ADVANCED")}>
    <Text style={{backgroundColor: this.state.backgroundColor}}>
        <Text style={styles.switchButtonsText}>ADVANCED</Text>
    </Text>
</TouchableOpacity>

selectionOnPress

selectionOnPress(userType) {
    this.setState({
        onClicked: true
    });
} 

props

constructor(props) {
    super(props);
    this.state = {
        onClicked: false
    }
    this.selectionOnPress = this.selectionOnPress.bind(this)
}

render(not adding the all codes, only added the useful codes for this post)

render() {
    var _style;
    if (this.state.onClicked) { // clicked button style
        _style = {
            backgroundColor: "red"
        }
    }
    else { // default button style
        _style = {
            backgroundColor: "blue"
        }
    }

I have 3 buttons in my react native app. When user clicks button 1 I need to change it color to orange. But other buttons should have the default color (grey). If user clicks the button 3 next time, color should change to orange ,but that 1st button color should reset to default. I'm totally new to react native and this is what I tried. But it applies for all buttons. I know if I can have multiple states with unique Id , it can be done. But I don't know the method.

<Text style={ styles.switchButtonsTitle }>Choose Type of User</Text>
<TouchableOpacity onPress={(userType) =>
    this.selectionOnPress("BASIC")} >
    <Text style={_style}>
        <Text style={styles.switchButtonsText}>BASIC</Text>
    </Text>
</TouchableOpacity>


<TouchableOpacity onPress={(userType) =>
    this.selectionOnPress("INTERMEDIATE")}>
    <Text style={_style}>
        <Text style={styles.switchButtonsText}>INTERMEDIATE</Text>
    </Text>
</TouchableOpacity>

<TouchableOpacity onPress={(userType) =>
    this.selectionOnPress("ADVANCED")}>
    <Text style={{backgroundColor: this.state.backgroundColor}}>
        <Text style={styles.switchButtonsText}>ADVANCED</Text>
    </Text>
</TouchableOpacity>

selectionOnPress

selectionOnPress(userType) {
    this.setState({
        onClicked: true
    });
} 

props

constructor(props) {
    super(props);
    this.state = {
        onClicked: false
    }
    this.selectionOnPress = this.selectionOnPress.bind(this)
}

render(not adding the all codes, only added the useful codes for this post)

render() {
    var _style;
    if (this.state.onClicked) { // clicked button style
        _style = {
            backgroundColor: "red"
        }
    }
    else { // default button style
        _style = {
            backgroundColor: "blue"
        }
    }
Share Improve this question edited Jan 25, 2018 at 15:13 Waleed Iqbal 1,3222 gold badges20 silver badges37 bronze badges asked Jan 25, 2018 at 12:57 apple_92apple_92 2632 gold badges7 silver badges18 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 12

I make some modification on your code

 export default class App extends Component {
constructor(props) {
    super(props);
    this.state = { selectedButton: null };
    this.selectionOnPress = this.selectionOnPress.bind(this);
}

selectionOnPress(userType) {
    this.setState({ selectedButton: userType });
}

render() {
    return (
        <View>
            <Text style={styles.switchButtonsTitle}>
                Choose Type of User
            </Text>
            <TouchableOpacity
                onPress={() => this.selectionOnPress("BASIC")}
            >
                <Text
                    style={{
                        backgroundColor:
                            this.state.selectedButton === "BASIC"
                                ? "red"
                                : "grey"
                    }}
                >
                    <Text style={styles.switchButtonsText}>BASIC</Text>
                </Text>
            </TouchableOpacity>

            <TouchableOpacity
                onPress={() => this.selectionOnPress("INTERMEDIATE")}
            >
                <Text
                    style={{
                        backgroundColor:
                            this.state.selectedButton === "INTERMEDIATE"
                                ? "red"
                                : "grey"
                    }}
                >
                    <Text style={styles.switchButtonsText}>
                        INTERMEDIATE
                    </Text>
                </Text>
            </TouchableOpacity>

            <TouchableOpacity
                onPress={() => this.selectionOnPress("ADVANCED")}
            >
                <Text
                    style={{
                        backgroundColor:
                            this.state.selectedButton === "ADVANCED"
                                ? "red"
                                : "grey"
                    }}
                >
                    <Text style={styles.switchButtonsText}>
                        INTERMEDIATE
                    </Text>
                </Text>
            </TouchableOpacity>
        </View>
    );
}
}


... don't forget to define your styles

First i remend you to create a button ponent. https://medium./the-react-native-log/organizing-a-react-native-project-9514dfadaa0

a easy way to do it is creating a state for each button like this

<TouchableOpacity onPress={
        (userType) => this.selectionOnPress("BASIC");
        this.setState({backgroundColor1: this.state.backgroundColor1 == 'grey'? 'orange' : 'grey'});
}>
    <Text style={{backgroundColor: this.state.backgroundColor1}}>
        <Text style={styles.switchButtonsText}>BASIC</Text> 
    </Text>
</TouchableOpacity>

<TouchableOpacity onPress={
    (userType) => this.selectionOnPress("INTERMEDIATE");
    this.setState({backgroundColor2: this.state.backgroundColor2 == 'grey'? 'orange' : 'grey'});
}>
    <Text style={{backgroundColor: this.state.backgroundColor2}>
        <Text style={styles.switchButtonsText}>INTERMEDIATE</Text> 
    </Text>
</TouchableOpacity>

<TouchableOpacity onPress={
    (userType) => this.selectionOnPress("ADVANCED");
    this.setState({backgroundColor3: this.state.backgroundColor3 == 'grey'? 'orange' : 'grey', backgroundColor1: 'grey'});
}>
    <Text style={{backgroundColor: this.state.backgroundColor}}>
        <Text style={styles.switchButtonsText}>ADVANCED</Text> 
    </Text>
</TouchableOpacity>

Simplified example:

//jsx
// asign default class name to your buttons with desired default color,
// onclick change said class to active css class which will have different background color
   <div>
    <button onClick={this.handleClick} className='btn-default'>basic</button>
    <button onClick={this.handleClick} className='btn-default'>intermediate</button>
    <button onClick={this.handleClick} className='btn-default'>advance</button>
  </div>

//js
// this method select parent element of your button which is also parent
// element for other buttons and then gets all other buttons in button 
// variable via children property
  handleClick(event) {
    const buttons = event.target.parentElement.children;
    for(let i = 0; i < buttons.length; i++) {
        //here you set all buttons to default color
        buttons[i].classList.add('btn-default');
        buttons[i].classList.remove('btn-active');
    }
    //here you add active class(color) to button you originally clicked
    event.target.classList.add('btn-active');
  }

  //css

.btn-default {
   background-color: grey;
 }

.btn-active {
   background-color: orange;
 }
发布评论

评论列表(0)

  1. 暂无评论