add lib integration entry; tweak 1 sentence

This commit is contained in:
Cheng Lou
2013-10-07 21:49:16 -04:00
committed by Connor McSheffrey
parent 6d2ea9a200
commit 7db760427c
4 changed files with 85 additions and 2 deletions
@@ -9,7 +9,7 @@ permalink: dom-event-listeners-tip.html
>
> This entry shows how to attach DOM events not provided by React ([check here for more info](/react/docs/cookbook/events.html)). This is good for integrations with other libraries such as jQuery.
This example displays the window width:
Try to resize the window:
```js
/** @jsx React.DOM */
+1 -1
View File
@@ -13,7 +13,7 @@ You want to listen to an event inside a component.
>
> This entry shows how to attach DOM events not provided by React ([check here for more info](/react/docs/cookbook/events.html)). This is good for integrations with other libraries such as jQuery.
This example displays the window width:
Try to resize the window:
```js
/** @jsx React.DOM */
@@ -0,0 +1,40 @@
---
id: external-libraries-integration-tip
title: Integration with external libraries
layout: docs
permalink: external-libraries-integration-tip.html
---
Let's try jQuery.
The general concept is simple: treat `componentDidMount` as jQuery's `ready` and tag your component with a `ref`. Then, call its `getDOMNode`. Notice that with `ref` (component scope), you don't need selectors anymore!
```js
/** @jsx React.DOM */
var ToggleBox = React.createClass({
componentDidMount: function() {
$(this.refs.dialogueBox.getDOMNode()).hide().fadeIn(500);
},
render: function() {
return (
<div>
<button onClick={this.handleClick}>Toggle me!</button>
<div
ref="dialogueBox"
style={{border: '1px solid gray', width: 90, height: 90, padding: 10}}
>
Easy!
</div>
</div>
);
},
handleClick: function() {
$(this.refs.dialogueBox.getDOMNode()).stop().toggle(200);
}
});
React.renderComponent(<ToggleBox />, mountNode);
```
Use libraries like jQuery for things like animation and AJAX, but preferably, don't manipulate the DOM with it. There are mostly likely better way to achieve that if you're already using React.
@@ -0,0 +1,43 @@
---
id: external-libraries-integration
title: Integration with external libraries
layout: docs
permalink: external-libraries-integration.html
---
### Problem
You want to incorporate React with jQuery for, say, its animation effects.
### Solution
The general concept is simple: treat `componentDidMount` as jQuery's `ready` and tag your component with a `ref`. Then, call its `getDOMNode`. Notice that with `ref` (component scope), you don't need selectors anymore!
```js
/** @jsx React.DOM */
var ToggleBox = React.createClass({
componentDidMount: function() {
$(this.refs.dialogueBox.getDOMNode()).hide().fadeIn(500);
},
render: function() {
return (
<div>
<button onClick={this.handleClick}>Toggle me!</button>
<div
ref="dialogueBox"
style={{border: '1px solid gray', width: 90, height: 90, padding: 10}}
>
Easy!
</div>
</div>
);
},
handleClick: function() {
$(this.refs.dialogueBox.getDOMNode()).stop().toggle(200);
}
});
React.renderComponent(<ToggleBox />, mountNode);
```
### Discussion
Use libraries like jQuery for things like animation and AJAX, but preferably, don't manipulate the DOM with it. There are mostly likely better way to achieve that if you're already using React.