@alapapa
@jblz
Easing Into React
On your existing WordPress
Jeff Bowen
Automattic
Topics
No JavaScript frameworks were created during the writing of this talk
What is React?
A JavaScript library
for building user interfaces
What makes React compelling
Declarative
Concentrate on what you want your application to look like instead of how to make it so.
Declarative views make your code more predictable and easier to debug.
Component-Based
Lets you split the UI into independent, reusable pieces, and think about each piece in isolation.
Composing components is intuitive and powerful.
Versatile
Library -- not a framework.
“Isomorphic” -- render on a node server as well as in the browser.
Useful in Native (read: “mobile”) app dev.
React is….”reactive”
Props (properties)
State
By default, components re-render on changes to their:
Composition
Combining simple functional components to build more complicated ones.
Like the usual composition of functions in mathematics, the result of each function is passed as the argument of the next, and the result of the last one is the result of the whole.
<ParentComponent>
<ChildComponent prop1=”value1”>
<GrandchildComponent prop2=”value2” />
</ChildComponent>
</ParentComponent>
JSX
Free and Open Source
<Code />
Getting Started
Node
npm
Node.js® is a JavaScript runtime built on Chrome's V8 JavaScript engine.
Usage ranges from a full-fledged web / network server to just running some one-off script.
The package manager for JavaScript and the world’s largest software registry.
“Almost half a million” packages
Together, they’re kind of a “Swiss Army Knife” for all things JavaScript
Used for a tremendous amount of tooling (web dev & otherwise)
Install node (& npm)
create-react-app
Integrating with your WordPress
Integrating with your WordPress
Integrating with your WordPress
Integrating with your WordPress
Integrating with your WordPress
// Something like…
add_action(
'admin_bar_menu',
function( $ab_menu ) {
$ab_menu->add_node( [
‘id’ => ‘custom_name,
] );
},
999
);
Integrating with your WordPress
> npm run build
Integrating with your WordPress
> mv build/static/js/main.0b89d4f5.js� $PLUGIN_DIR/js/appname.js
-- OR --
> sftp build/static/js/main.0b89d4f5.js�$YOUR_HOST:$PLUGIN_DIR/js/appname.js
Integrating with your WordPress
// As per usual
add_action( 'wp', function() {
$should_enqueue =
is_active_sidebar( $sidebar_name );
if ( $should_enqueue ) {
wp_enqueue_script( $script_slug,
plugins_url( 'js/appname.js',
__FILE__ ),
[], $version, $in_footer = true );
}
} );
Demo time!
Consider
@alapapa
@jblz
Questions or comments?
Jeff Bowen
Automattic