के बाद से प्रतिक्रिया mixins अब पदावनत कर रहे हैं, यहाँ का एक उदाहरण है एक उच्च आदेश घटक जो स्वीकार किए गए उत्तर में वर्णित वही कार्यक्षमता देने के लिए किसी अन्य घटक को लपेटता है। यह अनमाउंट पर किसी भी शेष टाइमआउट को साफ-साफ करता है, और बच्चे को घटक को एपीआई को प्रोप के माध्यम से प्रबंधित करने देता है।
यह ES6 कक्षाओं का उपयोग करता है और component composition जिसमें mixins को बदलने के लिए सिफारिश की रास्ता है 2017.
Timeout.jsx में
import React, { Component } from 'react';
const Timeout = Composition => class _Timeout extends Component {
constructor(props) {
super(props);
}
componentWillMount() {
this.timeouts = [];
}
setTimeout() {
this.timeouts.push(setTimeout.apply(null, arguments));
}
clearTimeouts() {
this.timeouts.forEach(clearTimeout);
}
componentWillUnmount() {
this.clearTimeouts();
}
render() {
const { timeouts, setTimeout, clearTimeouts } = this;
return <Composition
timeouts={timeouts}
setTimeout={setTimeout}
clearTimeouts={clearTimeouts}
{ ...this.props } />
}
}
export default Timeout;
MyComponent.jsx में
import React, { Component } from 'react';
import Timeout from './Timeout';
class MyComponent extends Component {
constructor(props) {
super(props)
}
componentDidMount() {
// You can access methods of Timeout as they
// were passed down as props.
this.props.setTimeout(() => {
console.log("Hey! I'm timing out!")
}, 1000)
}
render() {
return <span>Hello, world!</span>
}
}
// Pass your component to Timeout to create the magic.
export default Timeout(MyComponent);
यह अभी भी जेएस है, आप इसे वैसे ही करते हैं जैसा आप प्रतिक्रिया के बिना करेंगे। –
'clearTimeout' यह मान लेगा कि आप टाइमर आईडी स्टोर करते हैं। – WiredPrairie