Binding Methods In React Components
Solution 1:
You can only write valid HTML when generating Html from a string with react.
<input type='text' onInput=this.handleInput.bind(this, id)/>
is not valid: onInput
is not a HTML valid props
Generating a jsx string to HTML with react is a special case requiring babel.
I don't think you'll be able to insert jsx string in a component on the fly using a string but there is may be another solution. Split your string into an array and replace matching string by not a string code "<../>"
but by a jsx element <...>
. This jsx element will have the function binding as wanted to your component. I found a similar solution here: Replace part of string with tag in JSX
Solution 2:
There is a proper way to render a string with react:
Use dangerouslySetInnerHTML to inject HTML as a string in React. Subject is treated here
Regards
Solution 3:
You can set the bind key word at the end like below instead of the example you have given.
onInput=this.handleInput(id).bind(this)
or just call this method here and bind it at the constructor level.
onInput=this.handleInput(id)
constructor() {
super();
this.handleInput=this.handleInput.bind(this);
}
Post a Comment for "Binding Methods In React Components"