SolidJSS
SolidJS3y ago
46 replies
gubsey

Signals not updating in <For> tag

I have code that looks like this:
const [grid, updateGrid] = createSignal(new Array(10).fill([]).map(_ => new Array(10).fill(false).map(_ => Math.random() * 10 < 5)))

const flip = (x: number, y: number) => {
  let val = grid();
  val[y][x] = !val[y][x]
  updateGrid(val)
}

return (
  <table>
    <tbody>
      <For each={grid()}>{(row, y) => (
        <tr>
          <For each={row}>{(cell, x) => (
            <td
              class={`${cell ? "bg-green-500" : 'bg-red-500'} w-8 h-8 border border-separate`}
              onclick={_ => flip(x(), y())}></td>
          )}</For>
         </tr>
      )}</For>
    </tbody>
  </table>
);


This creates a random grid of red ang green squares. I want to be able to click any square and have the color flip. With logging, I found that the
grid
signal is updating, but the colors aren't changing. Am I completely misunderstanding signals here?
Was this page helpful?