1
1
# Dart wrapper for [ React JS] ( https://reactjs.org/ )
2
2
3
3
[ ![ Pub] ( https://img.shields.io/pub/v/react.svg )] ( https://pub.dev/packages/react )
4
- ![ ReactJS v16.10 .1] ( https://img.shields.io/badge/React_JS-v16.10 .1-green.svg )
4
+ ![ ReactJS v17.0 .1] ( https://img.shields.io/badge/React_JS-v17.0 .1-green.svg )
5
5
[ ![ Dart CI] ( https://github.com/Workiva/react-dart/workflows/Dart%20CI/badge.svg?branch=master )] ( https://github.com/Workiva/react-dart/actions?query=workflow%3A%22Dart+CI%22+branch%3Amaster )
6
6
[ ![ React Dart API Docs] ( https://img.shields.io/badge/api_docs-react-blue.svg )] ( https://pub.dev/documentation/react/latest/ )
7
7
@@ -11,7 +11,7 @@ _Thanks to the folks at [Vacuumlabs](https://www.vacuumlabs.com/) for creating t
11
11
12
12
### Installation
13
13
14
- If you are not familiar with the ReactJS library, read this [ react tutorial] ( http ://facebook.github.io/react /docs/getting-started.html) first.
14
+ If you are not familiar with the ReactJS library, read this [ react tutorial] ( https ://reactjs.org /docs/getting-started.html) first.
15
15
16
16
1 . Install the Dart SDK
17
17
@@ -25,9 +25,9 @@ If you are not familiar with the ReactJS library, read this [react tutorial](htt
25
25
name: your_package_name
26
26
version: 1.0.0
27
27
environment:
28
- sdk: ^2.0 .0
28
+ sdk: ^2.7 .0
29
29
dependencies:
30
- react: ^5 .0.0
30
+ react: ^6 .0.0
31
31
` ` `
32
32
33
33
3. Install the dependencies using pub:
@@ -123,10 +123,10 @@ var aButton = button({"onClick": (SyntheticMouseEvent event) => print(event)});
123
123
2. Then register the class so ReactJS can recognize it.
124
124
125
125
```dart
126
- var CoolWidget = registerComponent (() => new CoolWidgetComponent());
126
+ var CoolWidget = registerComponent2 (() => CoolWidgetComponent());
127
127
```
128
128
129
- > __Warning:__ `registerComponent ` should be called only once per component and lifetime of application.
129
+ > __Warning:__ `registerComponent2 ` should be called only once per component and lifetime of application.
130
130
131
131
3. Then you can use the registered component similarly as native elements.
132
132
@@ -159,7 +159,7 @@ class CoolWidgetComponent extends Component2 {
159
159
}
160
160
}
161
161
162
- var CoolWidget = registerComponent ( () => new CoolWidgetComponent ());
162
+ var CoolWidget = registerComponent2 ( () => CoolWidgetComponent ());
163
163
` ` `
164
164
165
165
` ` ` dart
@@ -181,14 +181,14 @@ main() {
181
181
182
182
> __Note:__ The typed interface capabilities of this library are fairly limited, and can result in
183
183
extremely verbose implementations. We strongly recommend using the
184
- [OverReact](https://pub.dartlang.org /packages/over_react) package - which
184
+ [OverReact](https://pub.dev /packages/over_react) package - which
185
185
makes creating statically-typed React UI components using Dart easy.
186
186
187
187
` ` ` dart
188
188
// cool_widget.dart
189
189
typedef CoolWidgetType({String headline, String text, int counter});
190
190
191
- var _CoolWidget = registerComponent ( () => new CoolWidgetComponent ());
191
+ var _CoolWidget = registerComponent2 ( () => CoolWidgetComponent ());
192
192
193
193
CoolWidgetType CoolWidget({String headline, String text, int counter}) {
194
194
return _CoolWidget({' headline' :headline, ' text' :text});
@@ -286,7 +286,7 @@ If you want to work with DOM nodes of dart or JS components instead,
286
286
you can call top level `findDOMNode` on anything the ref returns.
287
287
288
288
```dart
289
- var DartComponent = registerComponent (() => new _DartComponent());
289
+ var DartComponent = registerComponent2 (() => _DartComponent());
290
290
class _DartComponent extends Component2 {
291
291
@override
292
292
render() => div({});
@@ -296,16 +296,16 @@ class _DartComponent extends Component2 {
296
296
}
297
297
}
298
298
299
- var ParentComponent = registerComponent (() => new _ParentComponent());
299
+ var ParentComponent = registerComponent2 (() => _ParentComponent());
300
300
class _ParentComponent extends Component2 {
301
- InputElement inputRef; // Returns the DOM node.
302
- _DartComponent dartComponentRef; // Returns instance of _DartComponent
301
+ final inputRef = createRef<InputElement>() ; // inputRef.current is the DOM node.
302
+ final dartComponentRef = createRef<_DartComponent>() ; // dartComponentRef.current is the instance of _DartComponent
303
303
304
304
@override
305
305
void componentDidMount() {
306
- print(inputRef.value); // Prints "hello" to the console.
306
+ print(inputRef.current. value); // Prints "hello" to the console.
307
307
308
- dartComponentRef.someInstanceMethod(5); // Calls the method defined in _DartComponent
308
+ dartComponentRef.current. someInstanceMethod(5); // Calls the method defined in _DartComponent
309
309
react_dom.findDOMNode(dartComponentRef); // Returns div element rendered from _DartComponent
310
310
311
311
react_dom.findDOMNode(this); // Returns root dom element rendered from this component
@@ -314,26 +314,26 @@ class _ParentComponent extends Component2 {
314
314
@override
315
315
render() {
316
316
return div({},
317
- input({"ref": (ref){ inputRef = ref; } , "defaultValue": "hello"}),
318
- DartComponent({"ref": (ref) { dartComponentRef = ref; } }),
317
+ input({"ref": inputRef, "defaultValue": "hello"}),
318
+ DartComponent({"ref": dartComponentRef}),
319
319
);
320
320
}
321
321
}
322
322
```
323
323
324
324
### Example Application
325
325
326
- For more robust examples take a look at our [examples](https://github.com/cleandart /react-dart/tree/master/example).
326
+ For more robust examples take a look at our [examples](https://github.com/Workiva /react-dart/tree/master/example).
327
327
328
328
329
329
330
330
## Unit Testing Utilities
331
331
332
- [lib/react_test_utils.dart](https://github.com/cleandart /react-dart/blob/master/lib/react_test_utils.dart) is a
332
+ [lib/react_test_utils.dart](https://github.com/Workiva /react-dart/blob/master/lib/react_test_utils.dart) is a
333
333
Dart wrapper for the [ReactJS TestUtils](https://reactjs.org/docs/test-utils.html) library allowing for unit tests
334
334
to be made for React components in Dart.
335
335
336
- Here is an example of how to use React React.addons.TestUtils. within a Dart test.
336
+ Here is an example of how to use `package:react/react_test_utils.dart` within a Dart test.
337
337
338
338
```dart
339
339
import ' package:test/test.dart' ;
@@ -354,7 +354,7 @@ class MyTestComponent extends react.Component2 {
354
354
}
355
355
}
356
356
357
- var myTestComponent = react.registerComponent (() => new MyTestComponent());
357
+ var myTestComponent = react.registerComponent2 (() => new MyTestComponent());
358
358
359
359
void main() {
360
360
test(' should click button and set span text to " success" ' , () {
@@ -391,61 +391,36 @@ Format using
391
391
dartfmt -l 120 -w .
392
392
```
393
393
394
- While we' d like to adhere to the recommended line length of 80, it' s too too short for much of the code
394
+ While we' d like to adhere to the recommended line length of 80, it' s too short for much of the code
395
395
repo written before a formatter was use, causing excessive wrapping and code that' s hard to read.
396
396
397
- So, we us a line length of 120 instead.
397
+ So, we use a line length of 120 instead.
398
398
399
399
# ## Running Tests
400
400
401
- # ### Dart 2: dart2js
401
+ # ### dart2js
402
402
403
403
` ` ` bash
404
404
pub run test -p chrome
405
405
` ` `
406
406
_Or any other browser, e.g. ` -p firefox` ._
407
407
408
- # ### Dart 2: Dart Dev Compiler ("DDC")
408
+ # ### Dart Dev Compiler ("DDC")
409
409
410
410
` ` ` bash
411
411
pub run build_runner test -- -p chrome
412
412
` ` `
413
413
_DDC only works in chrome._
414
414
415
- # ### Dart 1: Dart VM
416
-
417
- ` ` ` bash
418
- pub run test -p content-shell
419
- ` ` `
420
-
421
- # ### Dart 1: dart2js
422
-
423
- ` ` ` bash
424
- pub run test -p chrome
425
- ` ` `
426
- _Or any other browser platform, e.g. ` -p firefox` ._
427
-
428
- # ### Dart 1: Dart Dev Compiler ("DDC")
429
-
430
- 1. In one terminal, serve the test directory using the dev compiler:
431
- ` ` ` bash
432
- pub serve test --port=8090 --web-compiler=dartdevc
433
- ` ` `
434
- 2. In another terminal, run the tests, referencing the port the dev compiler is using to serve the test directory:
435
- ` ` ` bash
436
- pub run test -p chrome --pub-serve=8090
437
- ` ` `
438
- _DDC only works in chrome._
439
-
440
415
# ## Building React JS Source Files
441
416
442
417
Make sure the packages you need are dependencies in ` package.json` then run:
443
418
` ` ` bash
444
- npm install
419
+ yarn install
445
420
` ` `
446
421
447
422
After modifying files any files in ./js_src/, run:
448
423
449
424
` ` ` bash
450
- npm run build
425
+ yarn run build
451
426
` ` `
0 commit comments