- A Web consultant: Full-Stack, clojure{,script}, ruby{, on rails}, javascript, react
(+ 1 2 3)
instead of 1 + 2 + 3
or +(1,2,3)
We should try to maximize the usage of pure components e.g. components written as functions with no state that depends only on their props.
ReactDOM.render(React.createElement(Square, {value: 42,
onClick: () => alert("clicked")}), klipse_container)
<Square
value={26}
onClick={() => alert("clicked")}
/>
You can do almost everything with JSX, but there are some pitfalls.
Be sure to check this interactive JSX guide
Exercise: display the value when the square is clicked
The react state is the single source of truth.
The state of the html element is updated by a react handler that listens to onChange
type of events.
<TextInput/>
Exercises:
1. Convert the input to upper case
2. Prepend input:
to the input
Sometimes (not too often!), we need to interact directly with the DOM e.g. for manipulating a canvas
element.
React allows us to get access to the DOM elements that are rendered.
<MyCanvas bgColor="blue"/>
Exercises:
1. Add a button that cleans the canvas
2. Add more params to props: fill color, point thickness…
3. Add an element that displays the number of points in the canvas
class SquareLogic extends React.Component {
constructor(props) {
super(props)
this.state = {
val: 10
}
}
increment() {
let state = R.assoc('val', this.state.val + 1, this.state)
this.setState(state)
}
render() {
return (
<Square value={this.state.val}
onClick= {() => this.increment()}/>
)
}
}
window.SquareLogic = SquareLogic
<SquareLogic/>
Exercises:
1. Draw 4 squares instead of 1
2. On odd squares, increment - on even squares, decrement
3. Display the number of clicks
4. Add a reset button
5. Add an undo button
Three principles (constraints):
Many Positive consequences:
Component
const mapDispatchToProps = (dispatch) => ({
onClick() {
dispatch(incrementSquareValue())
}
})
const mapStateToProps = (state) => ({
value: state.square.value
})
window.SquareRedux = ReactRedux.connect(mapStateToProps, mapDispatchToProps)(Square)
Actions
incrementSquareValue = () => ({
type: 'INCREMENT_SQUARE_VALUE'
})
Reducers
square = (state={value: 0}, action) => {
switch(action.type) {
case 'INCREMENT_SQUARE_VALUE':
return R.assoc('value', state.value + 1, state)
default:
return state
}
}
app = Redux.combineReducers({square: square})
The store
store = Redux.createStore(app)
The App
<ReactRedux.Provider store={store}>
<SquareRedux/>
</ReactRedux.Provider>
Playground
// store.dispatch(incrementSquareValue())
// store.getState()
Write a react redux application with:
1. A Game page: Tic Tac Toe
2. A Params page: Display groups of parameters as specified by a dynamic JSON. The parameters should be grouped according to the first part of their name. The required controls are: text input, number input, checkbox and dropdown.
3. A header that display the name of the current page and allow to switch pages
Remarks:
1. BootStrap your react project with Create React App
2. You must write unit tests for your reducers and your components with jest and enzyme
3. Use yarn
4. Use ramda for data manipulation
Here is an example of a JSON that defines the params
const params = [
{
"Name":"Basics.Title",
"Type":"TextInput",
"Value":"Hello World",
"Description":"Title of the book"
},
{
"Name":"Advanced.Color_Cover",
"Type":"Checkbox",
"Value":["Red", "Black"],
"Description":"Colors of the cover",
"ListValues":["Red","Green","Blue","Black"],
},
{
"Name":"Basics.Color",
"VariableType":"Dropdown",
"Value":"Red",
"Description":"Color of the font",
"ListValues":["Red","Green","Blue","Black"],
},
{
"Name":"Basics.budget",
"Type":"Number",
"Value":95.2,
"Description":"Budget",
"Min":1.0,
"Max":100.0,
"Step":0.5,
},
]
powered by Klipse /