Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
278 views
in Technique[技术] by (71.8m points)

javascript - 如何在React中访问DOM元素? 什么是React中document.getElementById()的等效项(How to access a DOM element in React? What is the equilvalent of document.getElementById() in React)

How do I select certain bars in react.js?(如何在react.js中选择某些栏?)

This is my code:(这是我的代码:) var Progressbar = React.createClass({ getInitialState: function () { return { completed: this.props.completed }; }, addPrecent: function (value) { this.props.completed += value; this.setState({ completed: this.props.completed }); }, render: function () { var completed = this.props.completed; if (completed < 0) { completed = 0 }; return (...); } I want to use this React component:(我想使用这个React组件:) var App = React.createClass({ getInitialState: function () { return { baction: 'Progress1' }; }, handleChange: function (e) { var value = e.target.value; console.log(value); this.setState({ baction: value }); }, handleClick10: function (e) { console.log('You clicked: ', this.state.baction); document.getElementById(this.state.baction).addPrecent(10); }, render: function () { return ( <div class="center">Progress Bars Demo <Progressbar completed={25} id="Progress1" /> <h2 class="center"></h2> <Progressbar completed={50} id="Progress2" /> <h2 class="center"></h2> <Progressbar completed={75} id="Progress3" /> <h2 class="center"></h2> <span> <select name='selectbar' id='selectbar' value={this.state.baction} onChange={this.handleChange}> <option value="Progress1">#Progress1</option> <option value="Progress2">#Progress2</option> <option value="Progress3">#Progress3</option> </select> <input type="button" onClick={this.handleClick10} value="+10" /> <button>+25</button> <button>-10</button> <button>-25</button> </span> </div> ) } }); I want to execute the handleClick10 function and perform the operation for my selected progressbar.(我想执行handleClick10函数并为我选择的进度栏执行操作。) But the result I get is:(但是我得到的结果是:) You clicked: Progress1 TypeError: document.getElementById(...) is null How do I select the certain Element in react.js?(如何在react.js中选择特定元素?)   ask by user504909 translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You can do that by specifying the ref(您可以通过指定ref)

<Progressbar completed={25} id="Progress1" ref="Progress1"/> <h2 class="center"></h2> <Progressbar completed={50} id="Progress2" ref="Progress2"/> <h2 class="center"></h2> <Progressbar completed={75} id="Progress3" ref="Progress3"/> In order to get the element just do(为了得到元素就做) var object = this.refs.Progress1; Remember to use this inside an arrow function block like:(记住要在箭头功能块中使用this例如:) print = () => { var object = this.refs.Progress1; } and so on...(等等...) EDIT(编辑) However facebook advises against it because string refs have some issues, are considered legacy, and are likely to be removed in one of the future releases.(但是,facebook建议不要这样做,因为字符串引用存在一些问题,被认为是旧问题,并且很可能在将来的发行版中被删除。) From the docs:(从文档:) Legacy API: String Refs(旧版API:字符串引用) If you worked with React before, you might be familiar with an older API where the ref attribute is a string, like "textInput", and the DOM node is accessed as this.refs.textInput.(如果您以前使用过React,那么您可能会熟悉一个较旧的API,其中ref属性是一个字符串,例如“ textInput”,并且DOM节点可以通过this.refs.textInput访问。) We advise against it because string refs have some issues, are considered legacy, and are likely to be removed in one of the future releases.(我们建议不要这样做,因为字符串引用存在一些问题,被认为是旧问题,并且很可能在将来的发行版中删除。) If you're currently using this.refs.textInput to access refs, we recommend the callback pattern instead.(如果您当前正在使用this.refs.textInput访问引用,则建议改用回调模式。) A recommended way for React 16.2 and earlier is to use the callback pattern:(对于React 16.2和更早版本,推荐的方法是使用回调模式:) <Progressbar completed={25} id="Progress1" ref={(input) => {this.Progress[0] = input }}/> <h2 class="center"></h2> <Progressbar completed={50} id="Progress2" ref={(input) => {this.Progress[1] = input }}/> <h2 class="center"></h2> <Progressbar completed={75} id="Progress3" ref={(input) => {this.Progress[2] = input }}/> DOC for using callback(DOC使用回调) In React 16.3+ , use React.createRef() to create your ref:(在React 16.3+中 ,使用React.createRef()创建引用:) class MyComponent extends React.Component { constructor(props) { super(props); this.myRef = React.createRef(); } render() { return <div ref={this.myRef} />; } } In order to access the element, use:(为了访问元素,请使用:) const node = this.myRef.current; DOC for using React.createRef()(使用React.createRef()的DOC)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...