reactJS道具和refs有什么区别?

Nur*_*ony 2 javascript reactjs

我对ReactJS props和感到困惑refs。任何人都可以通过适当的例子来解释它们之间的区别。提前致谢。

Fra*_*ard 5

道具用于传递应该是静态的参数(与状态相反)。例如,您可以将大小或名称从upperView传递给LowerView(嵌套视图);

道具有趣的部分:https://facebook.github.io/react/docs/transferring-props.html

ref用于访问真正的DOM,而不是react的虚拟DOM。当您需要访问真实的DOM时需要它。

这部分很有趣:https : //facebook.github.io/react/docs/more-about-refs.html

 this.setState({userInput: ''}, function() {
        // This code executes after the component is re-rendered
        React.findDOMNode(this.refs.theInput).focus();   // Boom! Focused!
      });
Run Code Online (Sandbox Code Playgroud)

上面的示例显示了状态更新后如何正确访问DOM元素。

希望能帮助到你。