class List extends Component {
<DoSomethingToList>
{this.props.list.map(item => <Item key={item} ref={item} />}
</DoSomethingToList>
}
class DoSomethingToList extends Component {
componentDidUpdate(prevProps) {
// I want to access refs here
let refs = this.refs
// refs === {} ------> Why is this happening?
}
render() {
<div>
{this.props.children}
</div>
}
}
我希望能够在包装器组件的生命周期方法中以引用方式访问子级。这样,我就可以跟踪动画的domNode的先前实例。但是,每当我访问引用时,它们都会作为一个空对象返回。在生命周期方法中访问React的React友好方法是什么?
答案 0 :(得分:0)
父/容器组件(DoSomethingToList
)应该跟踪引用。
const List = ({list}) => (
<DoSomethingToList>
{ctx => (
list.map((item) => <Item key={item} ref={ctx.refs[item]} />)
)}
</DoSomethingToList>
)
class DoSomethingToList extends Component {
state = {refs: []};
componentDidUpdate(prevProps, prevState) {
let refs = prevState.refs;
// do something with refs...
}
render() {
<div>
{this.props.children(this.state)}
</div>
}
}
老实说,这是一种不好的做法,因为React不会知道每次ref
所持有的元素状态是否有变化。
因此,我建议公开一个子组件可以调用的回调,以使父项将更改告知父项。
const List = ({list}) => (
<DoSomethingToList>
{ctx => (
list.map((item) =>
<Item key={item} value={ctx.values[item]} onUpdate={ctx.updateValue} />)
)}
</DoSomethingToList>
)
class DoSomethingToList extends Component {
state = {
values: [],
updateValue: this.updateValue
};
updateValue = (value, i) => {
const {values} = prevState;
const newValues = {...values};
newValues[i] = value;
this.setState({values: newValues});
}
componentDidUpdate(prevProps, prevState) {
const {values} = prevState;
// do something with values...
}
render() {
<div>
{this.props.children(this.state)}
</div>
}
}