简体   繁体   English

条件CSS无法在React Native中呈现

[英]Conditional CSS not rendering in React Native

I'm working on creating a chess game with React Native, but I'm having a hard time rendering the background colors for squares. 我正在使用React Native创建国际象棋游戏,但是我很难渲染正方形的背景颜色。 It's supposed to be like a regular chessboard in the sense that black and white alternate across rows. 在黑白交替跨行的意义上,它应该像普通的棋盘。 The game is loosely based off of a tutorial that was written in React, but I've rebuilt the front end to work with React Native. 该游戏大致基于React编写的教程,但我已经重建了前端以与React Native一起使用。 It's almost functional, but I'm stuck with this one issue. 它几乎可以正常工作,但是我仍然坚持这一问题。

I've tried moving around props and stylesheets, adjusting syntax, and doing that sort of thing but I'm not getting anywhere. 我曾尝试过移动道具和样式表,调整语法并做类似的事情,但是我什么也没做。

Here is my "square" component (Square.js) 这是我的“正方形”组件(Square.js)

export default function Square(props) {
    return (
        <TouchableOpacity style={styles.button} onPress={props.onPress}>
            <Image 
                style={{height: 25, width: 25}} 
                source={props.source}
            />
        </TouchableOpacity>
    );
}

const styles = StyleSheet.create({
    button: { 
        padding: 5,
        width: 35,
        fontSize: 24,
        lineHeight: 2,
        height: 35,
        marginRight: 1,
        marginTop: 1,
        backgroundColor: 'transparent',
    },
});

and here is my board component (Board.js) 这是我的板子组件(Board.js)

const styles = StyleSheet.create({
    boardRow: {
      flexDirection: "row",
      margin: 1,
    },
      darkSquare: {
        backgroundColor: 'black',
    },
      lightSquare: {
        backgroundColor: 'white',
    }
});

export default class Board extends React.Component {
  renderSquare(i, squareShade) {
    return <Square 
      piece = {this.props.squares[i]} 
      source = {this.props.squares[i]? this.props.squares[i].source : null}
      style = {[styles.button, this.props.styles]}
      shade = {squareShade}
      onPress={() => this.props.onPress(i)}
    />
  }

  render() {
    const board = [];
    for(let i = 0; i < 8; i++){
      const squareRows = [];
      for(let j = 0; j < 8; j++){
        const squareShade = (isEven(i) && isEven(j)) || (!isEven(i) && !isEven(j))? styles.lightSquare : styles.darkSquare;
        squareRows.push(this.renderSquare((i*8) + j, squareShade));
      }
      board.push(<View style={styles.boardRow}>{squareRows}</View>)
    }

    return (
      <View>
        {board}
      </View>
    );
  }
}

function isEven(num){
  return num % 2 == 0
}

I'm not really getting any error messages, but I'm under the impression that something isn't being passed correctly. 我并没有收到任何错误消息,但给人的印象是某些东西没有正确传递。 If I change the backgroundColor in Square.js, then it will make all of the squares on the board appear as that color but the conditional aspect of it is giving me problems. 如果我在Square.js中更改backgroundColor,那么它将使板上的所有正方形都显示为该颜色,但是其条件方面给我带来了问题。 Any help is appreciated! 任何帮助表示赞赏!

it is actually quite simple, maybe you should check your condition, try following code 这实际上很简单,也许您应该检查一下状况,然后尝试执行以下代码

const styles = {
  button: {
    width: 35,
    height: 35,
  },
};

const Square = (props) => {
  const { isEven } = props;
  const squareStyle = {
    backgroundColor: isEven ? '#000' : '#FFF',
    ...styles.button
  }

  // depending on the isEven prop background color is conditional
  return (
    <TouchableOpacity style={squareStyle}>
      <Image/>
    </TouchableOpacity>
  )
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM