This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import React, {useState, useMemo, useCallback} from 'react'; | |
| function App() { | |
| const [length, set_length] = useState(3); | |
| const [name, set_name] = useState('John Doe'); | |
| const on_name_changed = useCallback((e) => set_name(e.target.value), []); | |
| const on_length_changed = useCallback((e) => set_length(Number(e.target.value)), []); | |
| return ( |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import React, {useState, useMemo} from 'react'; | |
| function App() { | |
| const [length, set_length] = useState(3); | |
| const [name, set_name] = useState('John Doe'); | |
| return ( | |
| <> | |
| <input value={name} onChange={e => set_name(e.target.value)} /> | |
| <NameDisplay name={name}/> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import React, {useState} from 'react'; | |
| function App() { | |
| const [length, set_length] = useState(3); | |
| const [name, set_name] = useState('John Doe'); | |
| return ( | |
| <> | |
| <input value={name} onChange={e => set_name(e.target.value)} /> | |
| <NameDisplay name={name}/> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Declaring variables | |
| // ReasonML has immutable variables by default | |
| let name = 'Michael'; | |
| // Has the same syntax for object destructuring | |
| let {user, password} = request; | |
| // Control flow |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| let sum = (a, b) => a + b; | |
| // ReasonML knows that the + operator is only applicable to integers | |
| // (every type has its own operators), so this results in: | |
| let sum = (a: int, b: int): int => a + b; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function add(x, y) { | |
| return x + y; | |
| } | |
| // Type inference in TypeScript will result in this: | |
| function add(x: any, y: any):any { | |
| return x + y; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function abacaba() { | |
| return | |
| { | |
| a: 'b' | |
| } | |
| } | |
| console.log(abacaba()); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| x = 'abacaba'; // using an undeclared variable is not allowed in strict mode | |
| delete x; // deleting variables is also not allowed | |
| function(x1, x1) {} // duplicating argument names is not allowed | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // the function itself | |
| function getData(callback, errorCallback) { | |
| try { | |
| // Do some network/api stuff... | |
| callback(result) | |
| } catch (e) { | |
| errorCallback(e); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function Car(name, make) { | |
| // Here, this is not a reference to outer object | |
| // But a placeheloder object you can use to construct the | |
| // desired value | |
| this.name = name; | |
| this.make = make; | |
| // you do not have to return anything, as this is automatically returned | |
| } | |
| const myCar = new Car('Outback', 'Subaru'); |