简体   繁体   English

ReactJS:onClick更改元素

[英]ReactJS: onClick change element

I've just started learning React and have a question. 我刚开始学习React并有一个问题。

I want to do the following: 我想做以下事情:

If a user clicks on a paragraph I want to change the element to an input field that has the contents of the paragraph prefilled. 如果用户单击某个段落,我想将该元素更改为具有预填充段落内容的输入字段。 (The end goal is direct editing if the user has certain privileges) (如果用户具有某些特权,最终目标是直接编辑)

I'm come this far but am totally at a loss. 我走到这一步,但完全不知所措。

var AppHeader = React.createClass({
    editSlogan : function(){
        return (
            <input type="text" value={this.props.slogan} onChange={this.saveEdit}/>
         )
    },
    saveEdit : function(){
        // ajax to server
    },
    render: function(){
        return (
            <header>
                <div className="container-fluid">
                    <div className="row">
                        <div className="col-md-12">
                            <h1>{this.props.name}</h1>
                            <p onClick={this.editSlogan}>{this.props.slogan}</p>
                        </div>
                    </div>
                </div>
            </header>
        );
    }
});

How can I override the render from the editSlogan function? 如何从editSlogan函数覆盖渲染?

If I understand your questions correctly, you want to render a different element in case of an "onClick" event. 如果我正确理解您的问题,您希望在“onClick”事件的情况下呈现不同的元素。

This is a great use case for react states. 这是反应状态的一个很好的用例。

Take the following example 以下面的例子为例

React.createClass({ 
    getInitialState : function() {
       return { showMe : false };
    },
    onClick : function() {
       this.setState({ showMe : true} );
    },
    render : function() {
        if(this.state.showMe) { 
            return (<div> one div </div>);
        } else { 
            return (<a onClick={this.onClick}> press me </a>);
        } 
    }
})

This will change the components state, and makes React render the div instead of the a-tag. 这将改变组件状态,并使React呈现div而不是a-tag。 When a components state is altered(using the setState method), React calculates if it needs to rerender itself, and in that case, which parts of the component it needs to rerender. 当组件状态被改变时(使用setState方法),React计算它是否需要重新渲染自己,并且在这种情况下,它需要重新渲染组件的哪些部分。

More about states https://facebook.github.io/react/docs/interactivity-and-dynamic-uis.html 更多关于州https://facebook.github.io/react/docs/interactivity-and-dynamic-uis.html

You can solve it a little bit more clear way: 你可以用更清晰的方式解决它:

 class EditableLabel extends React.Component { constructor(props) { super(props); this.state = { text: props.value, editing: false }; this.initEditor(); this.edit = this.edit.bind(this); this.save = this.save.bind(this); } initEditor() { this.editor = <input type="text" defaultValue={this.state.text} onKeyPress={(event) => { const key = event.which || event.keyCode; if (key === 13) { //enter key this.save(event.target.value) } }} autoFocus={true}/>; } edit() { this.setState({ text: this.state.text, editing: true }) }; save(value) { this.setState({ text: value, editing: false }) }; componentDidUpdate() { this.initEditor(); } render() { return this.state.editing ? this.editor : <p onClick={this.edit}>{this.state.text}</p> } } //and use it like <EditableLabel value={"any external value"}/>; 

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

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