[英]How to reuse functionality of one component to another component in React
I am looking at patterns about reusing code in react and I came across this approach render props .我正在研究关于在 react 中重用代码的模式,我遇到了这种方法render props 。 One thing I don't understand here is how do you actually reuse a function, I understand how the state is being shared but if you want to reuse also some function how do you do that?
我在这里不明白的一件事是你如何实际重用一个函数,我理解状态是如何共享的,但是如果你还想重用一些函数,你怎么做? I am posting some sample code that I got from official site of React and added an extra function.
我发布了一些从 React 官方网站获得的示例代码,并添加了一个额外的功能。
class Cat extends React.Component {
render() {
const mouse = this.props.mouse;
return (
<img src="/cat.jpg" style={{ position: 'absolute', left: mouse.x, top: mouse.y }} />
);
}
}
class Mouse extends React.Component {
constructor(props) {
super(props);
this.handleMouseMove = this.handleMouseMove.bind(this);
this.state = { x: 0, y: 0 };
}
handleMouseMove(event) {
this.setState({
x: event.clientX,
y: event.clientY
});
}
doSomethingElse(){
return 1;
}
render() {
return (
<div style={{ height: '100%' }} onMouseMove={this.handleMouseMove}>
{/*
Instead of providing a static representation of what <Mouse> renders,
use the `render` prop to dynamically determine what to render.
*/}
{this.props.render(this.state)}
</div>
);
}
}
class MouseTracker extends React.Component {
render() {
return (
<div>
<h1>Move the mouse around!</h1>
<Mouse render={mouse => (
<Cat mouse={mouse} />
)}/>
</div>
);
}
}
So my question is how do I re-use doSomethingElse() in some other component for ex in this case in Cat component!所以我的问题是我如何在其他组件中重用doSomethingElse()在这种情况下在Cat组件中! Also I am having some difficulties breaking down this stm:
{this.props.render(this.state)}
how is this working?另外我在分解这个 stm 时遇到了一些困难:
{this.props.render(this.state)}
这是如何工作的?
Best way to implement this would be creating a separate global component which does this functionality.实现这一点的最佳方法是创建一个单独的全局组件来执行此功能。
Suppose in components.js , you have :假设在 components.js 中,你有:
export const doSomethingElse = () => {
return 1;
}
And wherever you want it you can do by ,无论你想要什么,你都可以通过,
In Cat.js :在 Cat.js 中:
import {doSomethingElse} from 'component.js';
doSomethingElse();
Even you can make like a view component like :即使你可以像一个视图组件一样:
export const displayTime = (time) => (
<View>
<Text>{time}</Text>
</View>
)
And you can import in Dog.js like :你可以像这样在 Dog.js 中导入:
import {displayTime} from 'component.js';
render(){
return(
<View>
{this.displayTime('12:00PM');}
</View>
)
}
Hope it helps.希望能帮助到你。 feel free for doubts
随意怀疑
In fact :实际上 :
It's important to remember that just because the pattern is called “render props” you don't have to use a prop named render to use this pattern.
重要的是要记住,仅仅因为模式被称为“渲染道具”,你不必使用名为渲染的道具来使用这个模式。 In fact, any prop that is a function that a component uses to know what to render is technically a “render prop”.
事实上,任何作为组件用来知道要渲染什么的函数的道具在技术上都是“渲染道具”。
{this.props.render(this.state)} // It will execute the function (mouse) => Cat mouse={mouse}/> and will instance the component Cat/> with props which are the states of the component Mouse/> {this.props.render(this.state)} // 它将执行函数 (mouse) => Cat mouse={mouse}/>并使用作为组件 Mouse 状态的 props 实例化组件 Cat/> />
For example, if you want to pass the function doSomethingElse() to the child component , you could do :例如,如果你想将函数 doSomethingElse() 传递给子组件,你可以这样做:
class Mouse extends React.Component { doSomethingElse(){ return 1; } render() { return ( <div style={{ height: '100%' }} onMouseMove={this.handleMouseMove}> {this.props.render(this.state, this.doSomethingElse)} </div> ); } } class MouseTracker extends React.Component { render() { return ( <div> <h1>Move the mouse around!</h1> <Mouse render={(mouse, doSomethingElse, ...props) => ( <Cat mouse={mouse} doSomethingElse={doSomethingElse} {...props} /> // Possibly others props if you want )}/> </div> );
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.