1

While learning to use MobX I wanted to update a string from an <input/>. I know that in Smart Components I can just use onChange={this.variable.bind(this)}, but I don't understand how I can do so in the following scenario:

const dumbComponent = observer(({ prop }) => {

  // prop is an object
  // destruct1 is a string, destruct2 is an array
  const { destruct1, destruct2 } = prop;
  const list = destruct2.map((item, key) => (<li key={key} >{item}</li>));

  return (
    <div>
      <h1>title</h1>
      <h2>{destruct1}</h2>
      // Relevent part start
      <input classname="destruct" value={destruct1.bind(this)} />
      // Relevent part end
      <ul>{list}</ul>
    </div>
  );
});

export default TodoList;

Can I bind the value of input to destruct somehow? Obviously, this code doesn't work. But I don't know what to do.

1 Answer 1

1

You could create an inline arrow function and alter the prop.destruct1 like this:

const dumbComponent = observer(({ prop }) => {
  const { destruct1, destruct2 } = prop;
  const list = destruct2.map((item, key) => <li key={key}>{item}</li>);

  return (
    <div>
      <h1>title</h1>
      <h2>{destruct1}</h2>
      <input
        classname="destruct"
        value={destruct1}
        onChange={e => prop.destruct1 = e.target.value}
      />
      <ul>{list}</ul>
    </div>
  );
});
Sign up to request clarification or add additional context in comments.

Comments

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.