0

I am writing a React component that makes my text editable. When clicked, it renders an input, when blurred it renders a text like before.

To make it work, I have to put a lot of attributes in my elements. I want to handle many types of inputs (text, number, date, textarea, ...) and I don't want to repeat this list all along.

Is there a way of writing this list only once and inject it in my elements?

Something like this:

const options = "value={this.props.value} placeholder={this.props.placeholder} onChange={this.props.onChange}  onBlur={this.stopEdit} ref={(e) => {this.eRef = e;}}";

Later used like that:

render() {
    return (<input type="text" {{options}} />);
}

I have read about dangerouslySetInnerHTML but that's not doing what I need.

Many thanks for your help!

2 Answers 2

2

Why you dont use like this

const options = {
  value: this.props.value,
  placeholder: this.props.placeholder,
  ...
}

and html = <input type="text" {...options}/>

or you can use

html = React.createElement('input', {type: 'text', ...options})
Sign up to request clarification or add additional context in comments.

1 Comment

I'm glad to help you.
0

If you're looking on how to make text in your element editable, perhaps attach the content-editable="true" attribute to the element.

Hope this helps!

1 Comment

That's useful, thanks. But I want to control how it is edited so I stick to my homemade component :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.