简体   繁体   English

两个子组件之间的接线

[英]Wiring between two child components

I'm learning React and I have a specific example of trying to wire two components together through an interface. 我正在学习React,我有一个具体的示例,试图通过接口将两个组件连接在一起。 I have read most of the parent/child, child/parent and sibling component communication stackoverflow related questions and can't seem to find a proper answer. 我已经阅读了大多数父/子,子/父母和同级组件通信stackoverflow相关的问题,似乎找不到合适的答案。

Say I have an App component that renders a Box and a Mover component. 说我有一个呈现Box和Mover组件的App组件。 Quite simply, the Box has a move(x,y) function that I'd like the Mover to interface. 简而言之,Box有一个move(x,y)函数,我希望Mover与之交互。 However, the Mover doesn't have to be a child of Box (or any other relationship) -- specifically, I don't want the Box to pass down its own methods to the Mover. 但是,Mover不必一定是Box的子级(或任何其他关系),特别是我不希望Box将自己的方法传递给Mover。

What I'd like to do in the App, is something like this: 我想要在App中执行的操作如下:

...
render: function() {
    return (
        <Box ref="box"/>
        <Mover box = {this.refs.box} />
    )
}
....

However, you can't use refs in render() as clearly indicated in the docs and the box refs isn't defined anyways until it's mounted (so the box props won't work). 但是,您不能像在文档中明确指出的那样在render()中使用refs,并且无论如何都不会定义box refs,直到它被安装(所以box props不会起作用)。

I've thought of using an intermediate object: 我想过要使用一个中间对象:

...
render: function() {
    var api = {box: undefined};
    return (
        <Box api={api}/>
        <Mover api={api} />
    )
}
....

Where Box sets itself in this.props.api in its componentDidMount method, but that's a little iffy. Box在其componentDidMount方法中的this.props.api中设置自己,但这有点不可靠。 Another option would be to use the refs callback in the App to do the wiring, but that requires ugly glue code. 另一个选择是使用App中的refs回调进行连接,但这需要难看的粘合代码。

Is there any other way to pass references to (yet uncreated) components between each other when rendering them? 呈现它们时,还有其他方法可以在彼此之间传递对(尚未创建的)组件的引用吗? Or is there some more general design principle that I'm not adhering to? 还是我不遵守一些更一般的设计原则?

For siblings, you generally need to move whatever state they both need to reference up into a common parent component and pass it down as props (or sideways into an external storage object - see react-training's Less Simple Communication lesson for an example) and pass it to them as props. 对于兄弟姐妹,您通常需要将它们都需要引用的任何状态上移到一个公共父组件中,然后将其作为道具传递(或横向传递到一个外部存储对象中-有关示例,请参见react-training的Less Simple Communication课程),然后传递它作为道具。

eg if the component rendering Box and Mover had the following state: 例如,如果组件渲染BoxMover具有以下状态:

getInitialState: function() {
  return {
    x: 0,
    y: 0
  }
}

Then you could pass Mover a callback function which updates it: 然后,您可以通过Mover回调函数来对其进行更新:

handleMove: function(x, y) {
  this.setState({x: x, y: y})
}

// within render() - you could also pass x and y if Mover needs them
<Mover onMove={this.handleMove}/>

// within Mover
this.props.onMove(newX, newY)

If you passed this state to Box as props, it wouldn't necessarily need an explicit move() method (depending on what it does) but could either use the props in its render() method (which would automatically re-render when the props change), or you could react to the x and y values changing by detecting the change with a componentWillReceiveProps() method: 如果您将此状态作为道具传递给Box ,则不一定需要显式的move()方法(取决于它的作用),但可以在其render()方法中使用道具render()当道具更改),或者您可以通过componentWillReceiveProps()方法检测更改来对xy值更改做出反应:

// within render()
<Box x={this.state.x} y={this.state.y}/>

// within Box
componentWillReceiveProps: function(nextProps) {
  if (nextProps.x !== this.props.x || nextProps.y !== this.props.y) {
    // ...
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM