Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/426.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,我有两门课,大部分是独立的 var User = React.createClass({ .... updateGameScore: function(){ this.game1.score = .... } render: function(){ return ( <div>{this.state.totalScore}</div>); }); var Game = React.createClass({ .... upda

我有两门课,大部分是独立的

var User = React.createClass({
    ....
    updateGameScore: function(){ this.game1.score = .... } 
    render: function(){ return ( <div>{this.state.totalScore}</div>);
});


var Game = React.createClass({
    ....
    updateUserScore:function(){ how to access/modify parent here??? },
    render: function(){ return ( <div>{this.state.score}</div>);
});
var User=React.createClass({
....
updateGameScore:function(){this.game1.score=..}
render:function(){return({this.state.totalScore});
});
var Game=React.createClass({
....
updateUserScore:function(){如何在此处访问/修改父对象??},
render:function(){return({this.state.score});
});

我需要能够在游戏分数更改时更新用户的totalScore,反之亦然,基于某些公式(此处不相关)。这些组件使得游戏作为子组件嵌套在用户中,但不能反之亦然。用户分数的更改可以通过使用
this.state(..)传递变量来更新游戏分数
,但是当游戏分数发生变化时,我可以用什么方法更新正确的家长用户分数(一次可以有多个用户)

您可以通过道具将处理函数从用户传递到游戏:

var User = React.createClass({
    ...
    handleScoreChange: function(newScore) {
        this.setState({totalScore: newScore});
    }

    render: function () {
        return (
            <Game handleScoreChange={this.handleScoreChange.bind(this)} />
        )
    }
});

var Game = React.createClass({
    ...
    updateUserScore: function() {
        this.props.handleScoreChange(this.state.score); 
    }
}
var User=React.createClass({
...
HandleCoreChange:函数(新闻核心){
this.setState({totalScore:newScore});
}
渲染:函数(){
返回(
)
}
});
var Game=React.createClass({
...
updateUserScore:函数(){
this.props.handleScoreChange(this.state.score);
}
}

您可以通过道具将处理函数从用户传递到游戏:

var User = React.createClass({
    ...
    handleScoreChange: function(newScore) {
        this.setState({totalScore: newScore});
    }

    render: function () {
        return (
            <Game handleScoreChange={this.handleScoreChange.bind(this)} />
        )
    }
});

var Game = React.createClass({
    ...
    updateUserScore: function() {
        this.props.handleScoreChange(this.state.score); 
    }
}
var User=React.createClass({
...
HandleCoreChange:函数(新闻核心){
this.setState({totalScore:newScore});
}
渲染:函数(){
返回(
)
}
});
var Game=React.createClass({
...
updateUserScore:函数(){
this.props.handleScoreChange(this.state.score);
}
}

您可以通过道具将处理函数从用户传递到游戏:

var User = React.createClass({
    ...
    handleScoreChange: function(newScore) {
        this.setState({totalScore: newScore});
    }

    render: function () {
        return (
            <Game handleScoreChange={this.handleScoreChange.bind(this)} />
        )
    }
});

var Game = React.createClass({
    ...
    updateUserScore: function() {
        this.props.handleScoreChange(this.state.score); 
    }
}
var User=React.createClass({
...
HandleCoreChange:函数(新闻核心){
this.setState({totalScore:newScore});
}
渲染:函数(){
返回(
)
}
});
var Game=React.createClass({
...
updateUserScore:函数(){
this.props.handleScoreChange(this.state.score);
}
}

您可以通过道具将处理函数从用户传递到游戏:

var User = React.createClass({
    ...
    handleScoreChange: function(newScore) {
        this.setState({totalScore: newScore});
    }

    render: function () {
        return (
            <Game handleScoreChange={this.handleScoreChange.bind(this)} />
        )
    }
});

var Game = React.createClass({
    ...
    updateUserScore: function() {
        this.props.handleScoreChange(this.state.score); 
    }
}
var User=React.createClass({
...
HandleCoreChange:函数(新闻核心){
this.setState({totalScore:newScore});
}
渲染:函数(){
返回(
)
}
});
var Game=React.createClass({
...
updateUserScore:函数(){
this.props.handleScoreChange(this.state.score);
}
}

在等待答案时,我又做了一次黑客攻击,对于那些希望能够在根级别调用所有公共函数的人来说,这个答案也很有用,也很有趣

  • 假设你有克罗特班

    var Croot = React.createClass({
        ...
        anyPublicFunction(val){
           ...
        }
    });
    
  • 将渲染结果指定给变量

    var rootComponent = React.render(
        <Croot >..users & games here ...</Croot>,
        document.getElementById('lobbyArea')
    );
    

  • 我在等待答案的同时做了另一个黑客攻击,对于那些希望能够在根级别调用所有公共函数的人来说,这个答案也很有用,也很有趣

  • 假设你有克罗特班

    var Croot = React.createClass({
        ...
        anyPublicFunction(val){
           ...
        }
    });
    
  • 将渲染结果指定给变量

    var rootComponent = React.render(
        <Croot >..users & games here ...</Croot>,
        document.getElementById('lobbyArea')
    );
    

  • 我在等待答案的同时做了另一个黑客攻击,对于那些希望能够在根级别调用所有公共函数的人来说,这个答案也很有用,也很有趣

  • 假设你有克罗特班

    var Croot = React.createClass({
        ...
        anyPublicFunction(val){
           ...
        }
    });
    
  • 将渲染结果指定给变量

    var rootComponent = React.render(
        <Croot >..users & games here ...</Croot>,
        document.getElementById('lobbyArea')
    );
    

  • 我在等待答案的同时做了另一个黑客攻击,对于那些希望能够在根级别调用所有公共函数的人来说,这个答案也很有用,也很有趣

  • 假设你有克罗特班

    var Croot = React.createClass({
        ...
        anyPublicFunction(val){
           ...
        }
    });
    
  • 将渲染结果指定给变量

    var rootComponent = React.render(
        <Croot >..users & games here ...</Croot>,
        document.getElementById('lobbyArea')
    );
    

  • 在像您这样的情况下,如果两个组件不共享一个可以合理地用于存储状态更改的公共父组件,那么您应该使用一个存储。直到最近,我还不熟悉这个概念,但对这些事情了解得比我多的人建议使用,因为它是架构的简化/简化版本。它只是一个存储和操作数据的地方,独立于任何一个组件,但可以根据需要访问这些组件


    编辑:通量和回流似乎都被优秀的元素所取代。

    在像您这样的情况下,两种成分没有一个共同的父元素可以合理地用于存储状态变化,那么您应该使用存储。直到最近,我还不熟悉这一概念,但对这些事情比我知道得多的人建议使用它,因为它是一个精简/简化的体系结构版本。它只是一个独立于任何一个组件存储和操作数据的地方,但可以根据需要访问这些组件


    编辑:通量和回流似乎都被优秀的元素所取代。

    在像您这样的情况下,两种成分没有一个共同的父元素可以合理地用于存储状态变化,那么您应该使用存储。直到最近,我还不熟悉这一概念,但对这些事情比我知道得多的人建议使用它,因为它是一个精简/简化的体系结构版本。它只是一个独立于任何一个组件存储和操作数据的地方,但可以根据需要访问这些组件


    编辑:通量和回流似乎都被优秀的元素所取代。

    在像您这样的情况下,两种成分没有一个共同的父元素可以合理地用于存储状态变化,那么您应该使用存储。直到最近,我还不熟悉这一概念,但对这些事情比我知道得多的人建议使用它,因为它是一个精简/简化的体系结构版本。它只是一个独立于任何一个组件存储和操作数据的地方,但可以根据需要访问这些组件

    编辑:通量和回流似乎都被超调了