0

I'm trying to make a tic-tac-toe game in react native. My board is represented by an array in the state:

state = {
    tablero : [
  [null, null, null], 
  [null, null, null], 
  [null, null, null]
],
}

My board component is:

<View>
    <View style={styles.fila}>
      <Text style={[styles.celda, {borderTopWidth: 0, borderLeftWidth: 0}]}>
        {this.state.tablero[0][0]}
      </Text>
      <Text style={[styles.celda, {borderTopWidth: 0}]}>
        {this.state.tablero[0][1]}
      </Text>
      <Text style={[styles.celda, {borderTopWidth: 0, borderRightWidth: 0}]}>
        {this.state.tablero[0][2]}
      </Text>  
    </View>
    <View style={styles.fila}>
      <Text style={[styles.celda, {borderLeftWidth: 0}]}>
        {this.state.tablero[1][0]}
      </Text>
      <Text style={styles.celda}>
        {this.state.tablero[1][1]}
      </Text>
      <Text style={[styles.celda, {borderRightWidth: 0}]}>
        {this.state.tablero[1][2]}
      </Text>  
    </View>
    <View style={styles.fila}>
      <Text style={[styles.celda, {borderBottomWidth: 0, borderLeftWidth: 0}]}>
        {this.state.tablero[2][0]}
      </Text>
      <Text style={[styles.celda, {borderBottomWidth: 0}]}>
        {this.state.tablero[2][1]}
      </Text>
      <Text style={[styles.celda, {borderBottomWidth: 0, borderRightWidth: 0}]}>
        {this.state.tablero[2][2]}
      </Text>  
    </View>
  </View>

I want to know how would I update my board when on press. I tried with spread operator but couldnt figure it out

1
  • can you show how you are rendering your board possibly using CodeSandbox for easy sharing? Commented Nov 17, 2018 at 15:59

2 Answers 2

2

This is how you can do this with spread operators.

state = {
    tablero : [
      [null, null, null], 
      [null, null, null], 
      [null, null, null]
    ],
}

// ---------- some code -----

function onPress({x, y}, newValue) {// press cell index
  this.setState({...state,
    tablero: Object.assign([],[...state.tablero], {
      [y]: Object.assign([], [...state.tablero[y]], {[x]: newValue})
    })
  });
}

// if we call, for example, onPress({x: 1, y: 2}, 1);
// Output state will be
// { tablero: [ [ null, null, null ], [ null, null, null ], [ null, 1, null ] ] }

Btw, great article here: https://medium.com/@giltayar/immutably-setting-a-value-in-a-js-array-or-how-an-array-is-also-an-object-55337f4d6702

Sign up to request clarification or add additional context in comments.

Comments

0

Here's a simple example to get you started:

import React from "react";
import ReactDOM from "react-dom";

class App extends React.Component {
  state = {
    table: [[0, 0, 0]]
  };

  toggle = (e, i) => {
    const { table } = this.state;
    table[0][i] = table[0][i] === 0 ? 1 : 0;
    console.log(table);
    this.setState({ table });
  };

  render() {
    return (
      <ul>
        {this.state.table[0].map((n, i) => (
          <li id={n} onClick={e => this.toggle(e, i)}>
            {n}
          </li>
        ))}
      </ul>
    );
  }
}

const rootElement = document.getElementById("root");
ReactDOM.render(<App />, rootElement);

CodeSandbox here.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.