Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 什么是;“收尾”;这篇文章的意思是什么?_Javascript_Reactjs - Fatal编程技术网

Javascript 什么是;“收尾”;这篇文章的意思是什么?

Javascript 什么是;“收尾”;这篇文章的意思是什么?,javascript,reactjs,Javascript,Reactjs,我正在学习反应模式-渲染道具。 所以我开始写这篇文章,但当我要写完的时候, 我发现自己无法理解“关闭”的含义 在页面底部: 在无法静态定义道具的情况下(例如,因为 需要关闭组件的道具和/或状态)应 而是扩展React.Component 我在此补充代码: 'use strict'; class Cat extends React.Component { render() { const mouse = this.props.mouse; return (

我正在学习反应模式-渲染道具。 所以我开始写这篇文章,但当我要写完的时候, 我发现自己无法理解“关闭”的含义

在页面底部:

在无法静态定义道具的情况下(例如,因为 需要关闭组件的道具和/或状态)应 而是扩展React.Component

我在此补充代码:

'use strict';

class Cat extends React.Component {
    render() {
        const mouse = this.props.mouse;
        return (
            <img src="image/cat.png" style={{ position: 'absolute', left: mouse.x, top: mouse.y }} />
        );
    }
}

class Mouse extends React.PureComponent {
    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
        })
    }

    render() {
        return (
            <div style={{ height: '100%' }} onMouseMove={this.handleMouseMove}>
                {this.props.children(this.state)}
            </div>
        )
    }
}

class MouseTracker extends React.Component {
    renderTheCat(mouse) {
        return <Cat mouse={mouse} />;
    }

    render() {
        return (
            <div style={{ height: '100%' }}>
                <h1>Move the mouse around!</h1>
                <Mouse>
                    {this.renderTheCat}
                </Mouse>
            </div>
        )
    }
}

ReactDOM.render(
    <MouseTracker />, document.getElementById('root')
);
“严格使用”;
类Cat.Component{
render(){
const mouse=this.props.mouse;
返回(
);
}
}
类鼠标扩展了React.PureComponent{
建造师(道具){
超级(道具);
this.handleMouseMove=this.handleMouseMove.bind(this);
this.state={x:0,y:0};
}
handleMouseMove(事件){
这是我的国家({
x:event.clientX,
y:event.clientY
})
}
render(){
返回(
{this.props.children(this.state)}
)
}
}
类MouseTracker扩展React.Component{
渲染集(鼠标){
返回;
}
render(){
返回(
移动鼠标!
{this.renderSecat}
)
}
}
ReactDOM.render(
,document.getElementById('root'))
);
根据本文,为了避免否定使用React.PureComponent的效果,我可以将render prop定义为prototype上的实例方法。 但是如果我不能静态地定义渲染属性,我应该使用React.Component。
我想知道在什么情况下,我不能静态地定义渲染道具。

听起来像是对
闭包的引用
-一个常见的javascript术语hi@JaromandaX,你有代码示例来描述道具不能静态定义的情况吗?我知道
结束
,但我看不出它影响我在这里编写代码的原因。对不起,我帮不了忙-没有问题的代码,所以回答中没有代码对不起,我用代码补充了问题。检查此答案: