When coding a Gutenberg block plugin for WordPress and you want to remove some of the <RichText> toolbar options on that block. You can set “withoutInteractiveFormatting” and then list the allowedFormats.
I’ve decided to learn how to use react.js in order to create my XD plugin. I felt the best way to start was probably to go through some of React’s basic tutorials and write down my notes as I go along.
Chrome and Firefox have addons for debugging and inspecting React.js componants.
“Immutability makes complex features much easier to implement. Later in this tutorial, we will implement a “time travel” feature that allows us to review the tic-tac-toe game’s history and “jump back” to previous moves. This functionality isn’t specific to games — an ability to undo and redo certain actions is a common requirement in applications. Avoiding direct data mutation lets us keep previous versions of the game’s history intact, and reuse them later.”
“Detecting changes in immutable objects is considerably easier. If the immutable object that is being referenced is different than the previous one, then the object has changed.”
this.state : refers to the current component this.props : refers to the parent component
Taking it Further
The React tutorial has a few suggestions for taking the tic tac toe game example further. I’m going to have a go at them and see if I can complete them.
Display the location for each move in the format (col, row) in the move history list.
I’m going to modify the history constant to also record the location of the move (so I don’t have to do things like comparing arrays which is probably more complicated).
class Game extends React.Component {
constructor(props) {
super(props);
this.state = {
history: [{
squares: Array(9).fill(null),
location: null,
}],
stepNumber: 0,
xIsNext: true,
};
}
// Rest is the same...
}
Then in the handleClick(i) I modified the setState to record the location.
Then I change the render() function in the Game class to the following:
render() {
const history = this.state.history;
const current = history[this.state.stepNumber];
const winner = calculateWinner(current.squares);
const moves = history.map((step, move) => {
const desc = move ?
'Go to move #' + move + ' Location: #' + history[move].location :
'Go to game start';
return (
<li key={move}>
<button onClick={() => this.jumpTo(move)}>{desc}</button>
</li>
);
});
// rest is the same....
}
So it is now displaying the location. However the challenge was to show the column and the row so I need to translate that number into the column and row.
I ended up creating an array of all the possible locations in handleClick()
This checks if the current move number matches the overall step number. If it does it adds some bold tags.
Things I noticed….
You don’t need to enclose the html in quotes The variable being referenced has to be in one of the parent elements (which makes sense) and it has to be passed from the parent element to the child element. For some reason the last desc doesn’t need brackets – I’m not sure why this is the case at present. I’m assuming it’s because there’s no html so the brackets just escape the variable from the html string? If there was html I suspect it would need brackets.
Rewrite Board to use two loops to make the squares instead of hardcoding them.
I found this a bit tricker to solve but my solution ended up as follows:
Added a variable to the state of the Game component
This should recieve the current row numer (e.g. 0, 1 or 2 for each of the 3 rows).
The counter will always start at the current row number times the total number of columbs. e.g.
If the number of columns is 3
0 * 3 = 0 // Start on Cell Number 0 1 * 3 = 3 // Start on Cell Number 3 2 * 3 = 6 // Start on Cell Number 6
A for loop is then run to create the number of columns specified. Each key is then calculated by the current Cell Number + the current column number. e.g
Row 0:
Cell Number Start 0 + Column 0 = Key 0 Cell Number Start 0 + Column 1 = Key 1 Cell Number Start 0 + Column 2 = Key 2
Row 1:
Cell Number Start 3 + Column 0 = Key 3 Cell Number Start 3 + Column 1 = Key 4 Cell Number Start 3 + Column 2 = Key 5
Row 2:
Cell Number Start 6 + Column 0 = Key 6 Cell Number Start 6 + Column 1 = Key 7 Cell Number Start 6 + Column 2 = Key 8
Each column then adds a square with the relevant key to an array. Finally the finished row is returned by the function.
Added a function called renderRows to the Board component:
renderRows() {
let gameBoard = [];
for(let i=0; i < this.props.rows; i++) {
gameBoard.push(this.renderRow(i));
}
return gameBoard;
}
For each row the renderRow function is run and adds the result to the gameBoard array which is then returned by the function.
Finally I edited the render function to the below:
Then I edited the Render Square function so the code sets the id and the className. I’m not sure which I’m going to use but this will give me a couple to try.