Reactjs如何将值绑定到现有的输入元素

有一个html页面包含以下html并包含一个js文件


html:


<input type="text" name="customValue" id="customValue" value="">

.js 文件:


    class Calc extends React.Component {

      constructor() {

        super();

      }


      render() {

        return (99)

      }   

    }

    ReactDOM.render(<Calc />, document.getElementById("customValue"));

所以我的问题是如何使用 reactjs 将 customValue 设置为 99?


一只斗牛犬
浏览 131回答 1
1回答

紫衣仙女

你可以这样做: -HTML<div id="rootDiv"></div>JSclass Calc extends React.Component {&nbsp; &nbsp; &nbsp; constructor() {&nbsp; &nbsp; &nbsp; &nbsp; super();&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; getValue = () => {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return 99;&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; render() {&nbsp; &nbsp; &nbsp; &nbsp; const value = this.getValue();&nbsp; &nbsp; &nbsp; &nbsp; return (&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <input type="text" name="customValue" id="customValue" value={value}>&nbsp; &nbsp; &nbsp; &nbsp; )&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp;}ReactDOM.render(<Calc />, document.getElementById("rootDiv"));
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript