question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

React: Create a Controlled Input

See original GitHub issue

Challenge Name

React: Create a Controlled Input

Issue Description

I think I have the correct code and it is working like it is supposed to, but I’m not passing the test.

Browser Information

Chrome 64 (64 Bit) Windows 10 Desktop

Your Code

class ControlledInput extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      input: ''
    };
    // change code below this line
    this.handleChange = this.handleChange.bind(this);
    // change code above this line
  }
  // change code below this line
  handleChange(event){
    this.setState({
      input: event.target.value
    })
  }
  // change code above this line
  render() {
    return (
      <div>
        { /* change code below this line */}
        <input onChange={this.handleChange} />
        { /* change code above this line */}
        <h4>Controlled Input:</h4>
        <p>{this.state.input}</p>
      </div>
    );
  }
};

Screenshot

screenshot 2018-02-12 22 35 57

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
bqnguyen94commented, Feb 13, 2018

You forgot to add value to input:

In the render method, create the input element above the h4 tag. Add a value attribute which is equal to the input property of the component’s state. Then add an onChange() event handler set to the handleChange() method.

0reactions
blackboldsecommented, Sep 15, 2022

First step will be making a function that changes the state variable input.

Add a value attribute which is equal to the input property of the component’s state. Then add an onChange() event handler set to the handleChange() method.

Remember to bind this to the function.

And you can try with solution if you forget bindings in the constructor:

Hope reply for help.

Enjoy!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Forms - React
An input form element whose value is controlled by React in this way is called a “controlled component”. Since the value attribute is...
Read more >
React Controlled Components, the Hooks Way - Dmitri Pavlutin
The controlled component is a convenient technique to access the value of input fields in React. It doesn't use references and serves as...
Read more >
Controlled and uncontrolled form inputs in React don't have to ...
A controlled input accepts its current value as a prop, as well as a callback to change that value. You could say it's...
Read more >
React: Creating a Controlled Input - DEV Community ‍ ‍
There's a skeleton of a component called ControlledInput to create a controlled input element. Component's state as a input property with a ...
Read more >
What are controlled inputs with React? - Educative.io
The official documentation defines controlled inputs as: The React component that renders an element also controls what happens in that element on subsequent ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found