Dart wrapper for React JS
Thanks to the folks at Vacuumlabs for creating this project! ❤️
If you are not familiar with the ReactJS library, read this react tutorial first.
Install the Dart SDK
brew install dart
Create a
pubspec.yamlfile in the root of your project, and addreactas a dependency:name: your_package_nameversion: 1.0.0environment: sdk: ^2.11.0dependencies: react: ^6.0.0
Install the dependencies using pub:
dart pub get
In a .html file, include the javascript libraries
(provided with this library for compatibility reasons) within your .html file,
and also add an element with an id to mount your React component.
This package now supports both React 17 and React 18. To opt into React 18, replace usages of this package's JS files with their new, React 18 versions (see table below).
The React 17 JS files are now deprecated, and will be removed in the next major version of this package, 8.0.0.
| Mode | Library | JS File Name |
|---|---|---|
| Development | React & ReactDOM | packages/react/js/react.dev.js |
| Production | React & ReactDOM | packages/react/js/react.min.js |
| Mode | Library | JS File Name |
|---|---|---|
| Development | React | packages/react/react.js |
| Development | ReactDOM | packages/react/react_dom.js |
| Production | React & ReactDOM | packages/react/react_with_react_dom_prod.js |
| Production | React | packages/react/react_prod.js |
| Production | ReactDOM | packages/react/react_dom_prod.js |
Lastly, add the .js file that Dart will generate. The file will be the name of the .dart file that contains your main entrypoint, with .js at the end.
<html><head><!-- ... --></head><body><divid="react_mount_point">Here will be react content</div><scriptsrc="packages/react/js/react.dev.js"></script><scriptdefersrc="your_dart_file_name.dart.js"></script></body></html>Note: When serving your application in production, use
packages/react/js/react.min.jsfile instead of the un-minifiedreact.dev.jsshown in the example above.
Once you have an .html file containing the necessary .js files, you can initialize React
in the main entrypoint of your Dart application.
import'dart:html';
import'package:react/react.dart';
import'package:react/react_dom.dart'as react_dom;
main() {
// Something to render... in this case a simple <div> with no props, and a string as its children.var component =div({}, "Hello world!");
// Render it into the mount node we created in our .html file.
react_dom.render(component, querySelector('#react_mount_point'));
}If you are familiar with React (without JSX extension) React-dart shouldn't surprise you much. All elements are defined as
functions that take props as first argument and children as optional second argument. props should implement Map and children is either one React element or List with multiple elements.
var aDiv =div({"className":"something"}, [
h1({"style": {"height":"20px"}}, "Headline"),
a({"href":"something.com"}, "Something"),
"Some text"
]);For event handlers you must provide function that takes a SyntheticEvent(defined in this library).
var aButton =button({"onClick": (SyntheticMouseEvent event) =>print(event)});Define custom class that extends Component2 and implements - at a minimum -
render.// cool_widget.dartimport'package:react/react.dart'; classCoolWidgetComponentextendsComponent2 { render() =>div({}, "CoolWidgetComponent"); }
Then register the class so ReactJS can recognize it.
varCoolWidget=registerComponent2(() =>CoolWidgetComponent());
Warning:
registerComponent2should be called only once per component and lifetime of application.Then you can use the registered component similarly as native elements.
// app.dartimport'dart:html'; import'package:react/react.dart'; import'package:react/react_dom.dart'as react_dom; import'cool_widget.dart'; main() { react_dom.render(CoolWidget({}), querySelector('#react_mount_point')); }
// cool_widget.dartimport'package:react/react.dart';
classCoolWidgetComponentextendsComponent2 {
@overriderender() {
returndiv({}, props['text']);
}
}
varCoolWidget=registerComponent2(() =>CoolWidgetComponent());// app.dartimport'dart:html';
import'package:react/react.dart';
import'package:react/react_dom.dart'as react_dom;
import'cool_widget.dart';
main() {
react_dom.render(CoolWidget({"text":"Something"}), querySelector('#react_mount_point'));
}Note: The typed interface capabilities of this library are fairly limited, and can result in extremely verbose implementations. We strongly recommend using the OverReact package - which makes creating statically-typed React UI components using Dart easy.
// cool_widget.darttypedefCoolWidgetType({String headline, String text, int counter});
var_CoolWidget=registerComponent2(() =>CoolWidgetComponent());
CoolWidgetTypeCoolWidget({String headline, String text, int counter}) {
return_CoolWidget({'headline':headline, 'text':text});
}
classCoolWidgetComponentextendsComponent2 {
Stringget headline => props['headline'];
Stringget text => props['text'];
intget counter => props['counter'];
@overriderender() {
returndiv({},
h1({}, headline),
span({}, text),
span({}, counter),
);
}
}// app.dartimport'dart:html';
import'package:react/react.dart';
import'package:react/react_dom.dart'as react_dom;
import'cool_widget.dart';
voidmain() {
react_dom.render(
myComponent(
headline:"My custom headline",
text:"My custom text",
counter:3,
),
querySelector('#react_mount_point')
);
}The Component2 class mirrors ReactJS' React.Component class, and contains all the same methods.
See: ReactJS Lifecycle Method Documentation for more information.
classMyComponentextendsComponent2 {
@overridevoidcomponentWillMount() {}
@overridevoidcomponentDidMount() {}
@overridevoidcomponentWillReceiveProps(Map nextProps) {}
@overridevoidcomponentWillUpdate(Map nextProps, Map nextState) {}
@overridevoidcomponentDidUpdate(Map prevProps, Map prevState) {}
@overridevoidcomponentWillUnmount() {}
@overrideboolshouldComponentUpdate(Map nextProps, Map nextState) =>true;
@overrideMapgetInitialState() => {};
@overrideMapgetDefaultProps() => {};
@overriderender() =>div({}, props['text']);
}The use of component refs in react-dart is a bit different from React JS.
- You can specify a ref name in component props and then call ref method to get the referenced element.
- Return values for Dart components, DOM components and JavaScript components are different.
- For a Dart component, you get an instance of the Dart class of the component.
- For primitive components (like DOM elements), you get the DOM node.
- For JavaScript composite components, you get a
ReactElementrepresenting the react component.
If you want to work with DOM nodes of dart or JS components instead,
you can call top level findDOMNode on anything the ref returns.
varDartComponent=registerComponent2(() =>_DartComponent());
class_DartComponentextendsComponent2 {
@overriderender() =>div({});
voidsomeInstanceMethod(int count) {
window.alert('count: $count');
}
}
varParentComponent=registerComponent2(() =>_ParentComponent());
class_ParentComponentextendsComponent2 {
final inputRef =createRef<InputElement>(); // inputRef.current is the DOM node.final dartComponentRef =createRef<_DartComponent>(); // dartComponentRef.current is the instance of _DartComponent@overridevoidcomponentDidMount() {
print(inputRef.current.value); // Prints "hello" to the console.
dartComponentRef.current.someInstanceMethod(5); // Calls the method defined in _DartComponent
react_dom.findDOMNode(dartComponentRef); // Returns div element rendered from _DartComponent
react_dom.findDOMNode(this); // Returns root dom element rendered from this component
}
@overriderender() {
returndiv({},
input({"ref": inputRef, "defaultValue":"hello"}),
DartComponent({"ref": dartComponentRef}),
);
}
}For more robust examples take a look at our examples.
lib/react_test_utils.dart is a Dart wrapper for the ReactJS TestUtils library allowing for unit tests to be made for React components in Dart.
Here is an example of how to use package:react/react_test_utils.dart within a Dart test.
import'package:test/test.dart';
import'package:react/react.dart'as react;
import'package:react/react_dom.dart'as react_dom;
import'package:react/react_test_utils.dart'as react_test_utils;
classMyTestComponentextends react.Component2 {
@overrideMapgetInitialState() => {'text':'testing...'};
@overriderender() {
return react.div({},
react.button({'onClick': (_) =>setState({'text':'success'})}),
react.span({'className':'spanText'}, state['text']),
);
}
}
var myTestComponent = react.registerComponent2(() =>newMyTestComponent());
voidmain() {
test('should click button and set span text to "success"', () {
var component = react_test_utils.renderIntoDocument(myTestComponent({}));
// Find button using tag namevar buttonElement = react_test_utils.findRenderedDOMComponentWithTag(
component, 'button');
// Find span using class namevar spanElement = react_test_utils.findRenderedDOMComponentWithClass(
component, 'spanText');
var buttonNode = react_dom.findDOMNode(buttonElement);
var spanNode = react_dom.findDOMNode(spanElement);
// Span text should equal the initial stateexpect(spanNode.text, equals('testing...'));
// Click the button and trigger the onClick event
react_test_utils.Simulate.click(buttonNode);
// Span text should change to 'success'expect(spanNode.text, equals('success'));
});
}Format using
dart format -l 120 .While we'd like to adhere to the recommended line length of 80, it's too short for much of the code repo written before a formatter was use, causing excessive wrapping and code that's hard to read.
So, we use a line length of 120 instead.
dart run build_runner test --release -- --preset dart2jsNOTE: When using Dart SDK < 2.14.0, use
--preset dart2js-legacyinstead.
dart run build_runner test -- --preset dartdevcNOTE: When using Dart SDK < 2.14.0, use
--preset dartdevc-legacyinstead.
Make sure the packages you need are dependencies in package.json then run:
yarn installAfter modifying files any files in ./js_src/, run:
yarn run build