# State and component - React

**URL:** <https://forum.freecodecamp.org/t/state-and-component-react/71718>\
**Category:** General\
**Created:** [January 2, 2017, 8:45pm UTC](https://forum.freecodecamp.org/t/state-and-component-react/71718 "2017-01-02T20:45:14Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![xavierartot](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/xavierartot/32/160720_2.png) [@xavierartot](https://forum.freecodecamp.org/u/xavierartot)\
**Post date:** [January 2, 2017, 8:45pm UTC](https://forum.freecodecamp.org/t/state-and-component-react/71718/1 "2017-01-02T20:45:14Z")

</div>

HI,  
I have a warning and the input doesn’t appear?

`23:3 warning Expected an assignment or function call and instead saw an expression no-unused-expressions`

the line 23 is

```
import React from 'react';

class Relation extends React.Component {
  constructor() {
    super()
    this.state = {
      txt : 'this the state txt',
    }
  }
  update(e) {
    this.setState({txt: e.target.value});
  }
  render() {
    return (
      <div>
        <h1> {this.state.txt} </h1>
        <Widget update={this.update.bind(this)} />
      </div>
    );
  }
}
const Widget = (props) => {//arguments props it's update prop
  <input type='text' onChange={props.update} />
}

export default Relation 

```

import React from ‘react’;  
import ReactDOM from ‘react-dom’;  
//import Props from ‘./Props’;  
//import App from ‘./App’;  
//import State from ‘./State’; //video 5  
import Relation from ‘./Relation-component’; //video 6  
[//relation-component.js](https://relation-component.js)

```
ReactDOM.render(
  <div>
    {/*<App att='create by a another value'>*/}
      {/*</App>*/}
    {/* video 4 */}
    {/*<Props cat={8} att='create by me Xaviou'>*/}
      {/*</Props>*/}
    {/* video 5 */}
    {/*<State /> */}
    {/* video 6 */}
    <Relation />
  </div>,
  document.getElementById('root')
);
```

---

<div class="post-metadata">

**Author:** ![PortableStick](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/portablestick/32/65591_2.png) [@PortableStick](https://forum.freecodecamp.org/u/PortableStick)\
**Post date:** [January 2, 2017, 9:05pm UTC](https://forum.freecodecamp.org/t/state-and-component-react/71718/2 "2017-01-02T21:05:02Z")

</div>

> [@xavierartot](#):
>
> ```auto
> update(e) {
> this.setState({txt: e.target.value});
> }
> 
> ```

I’m _pretty sure_ that `update()` isn’t a method on the component class, so you’ll have to declare it as a function.

```auto
function update(e) {
    this.setState({txt: e.target.value});
  }

```

---

<div class="post-metadata">

**Author:** ![xavierartot](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/xavierartot/32/160720_2.png) [@xavierartot](https://forum.freecodecamp.org/u/xavierartot)\
**Post date:** [January 3, 2017, 3:53pm UTC](https://forum.freecodecamp.org/t/state-and-component-react/71718/3 "2017-01-03T15:53:34Z")

</div>

I aggree with you for update but with ‘function’ :

```
Error in ./src/Relation-component.js
Syntax error: Unexpected token, expected ( (10:11)

   8 | }
   9 | }
> 10 | function update(e) {
     | ^
  11 | this.setState({txt: e.target.value});
  12 | }
  13 | render() {

 @ ./src/index.js 16:25-56
```

---

<div class="post-metadata">

**Author:** ![xavierartot](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/xavierartot/32/160720_2.png) [@xavierartot](https://forum.freecodecamp.org/u/xavierartot)\
**Post date:** [January 3, 2017, 4:18pm UTC](https://forum.freecodecamp.org/t/state-and-component-react/71718/4 "2017-01-03T16:18:32Z")

</div>

I take off the curly bracket and is working?  
What the difference here?

```
let Widget = (props) => 
  <input type='text' onChange={props.update} />

```

Edit: it’s a component syntax with one line, a short way to write a compenent.

---

<div class="post-metadata">

**Author:** ![PortableStick](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/portablestick/32/65591_2.png) [@PortableStick](https://forum.freecodecamp.org/u/PortableStick)\
**Post date:** [January 3, 2017, 6:00pm UTC](https://forum.freecodecamp.org/t/state-and-component-react/71718/5 "2017-01-03T18:00:02Z")

</div>

It took me a bit to figure out what you had changed. Your original code would work just fine, but you need to add a return statement:

```auto
let Widget = (props) => {
  return (<input type='text' onChange={props.update} />);
}

```

Those curly braces indicate a function block with no implicit return. The way you’re writing an arrow function implies the return of what follows the arrow, and it’s the cleanest way you can write a pure function in ES6. 👍

---

<div class="post-metadata">

**Author:** ![xavierartot](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/xavierartot/32/160720_2.png) [@xavierartot](https://forum.freecodecamp.org/u/xavierartot)\
**Post date:** [January 3, 2017, 6:36pm UTC](https://forum.freecodecamp.org/t/state-and-component-react/71718/6 "2017-01-03T18:36:14Z")

</div>

thanks, that weird in one line function I don’t need the return statement:slight\_smile:

Edit: I didn’t know for the implicit return…

the return statement works too without the parentheses if is outside a class.

```
return <h1>Title : {props.text}</h1>
```

---

<div class="post-metadata">

**Author:** ![PortableStick](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/portablestick/32/65591_2.png) [@PortableStick](https://forum.freecodecamp.org/u/PortableStick)\
**Post date:** [January 3, 2017, 6:51pm UTC](https://forum.freecodecamp.org/t/state-and-component-react/71718/7 "2017-01-03T18:51:11Z")

</div>

I don’t think parentheses are ever required, but it’s frequently used with JSX. It’s just a style thing.

---

<div class="post-metadata">

**Author:** ![Oxyrus](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/oxyrus/32/69509_2.png) [@Oxyrus](https://forum.freecodecamp.org/u/Oxyrus)\
**Post date:** [January 3, 2017, 7:03pm UTC](https://forum.freecodecamp.org/t/state-and-component-react/71718/8 "2017-01-03T19:03:11Z")

</div>

But it’s preferred to use parentheses because he’s returning JSX (like the render method)

---

<div class="post-metadata">

**Author:** ![ILM](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/ilm/32/363239_2.png) [@ILM](https://forum.freecodecamp.org/u/ILM)\
**Post date:** [January 16, 2021, 11:33am UTC](https://forum.freecodecamp.org/t/state-and-component-react/71718/9 "2021-01-16T11:33:25Z")

</div>

Closed on January 16, 2021, 11:33am UTC


