Register a SA Forums Account here!
JOINING THE SA FORUMS WILL REMOVE THIS BIG AD, THE ANNOYING UNDERLINED ADS, AND STUPID INTERSTITIAL ADS!!!

You can: log in, read the tech support FAQ, or request your lost password. This dumb message (and those ads) will appear on every screen until you register! Get rid of this crap by registering your own SA Forums Account and joining roughly 150,000 Goons, for the one-time price of $9.95! We charge money because it costs us money per month for bills, and since we don't believe in showing ads to our users, we try to make the money back through forum registrations.
 
  • Post
  • Reply
Ethereal
Mar 8, 2003
I highly recommend RethinkDB over MongoDB. They take data loss really seriously.

Adbot
ADBOT LOVES YOU

Ethereal
Mar 8, 2003

Helicity posted:

For those React people, is there a cleaner way of accomplishing the following? If calling an event handler with a parameter, you need to wrap it in an anonymous function and it looks horrid in more complex examples:

code:

var self = this;
var index = 1;
return (<input type="text" onChange={function() { self.handleBlockNameChange(index); }} />)

opposed to a parameterless call:

code:

return (<input type="text" onChange={self.handleBlockNameChange} />)

Two ways, use partial application through the Function.bind syntax or you can pass the parameter and function as props and have a handler that calls your passed in handler with the index.

Ethereal
Mar 8, 2003

spacebard posted:

I'm finally getting to play with react and redux. I'm seeing a lot of the following (in the Todo List Example for instance):

code:
const TodoList = ({ todos, onTodoClick }) => (
  <ul>
    {todos.map(todo =>
      <Todo
        key={todo.id}
        {...todo}
        onClick={() => onTodoClick(todo.id)}
      />
    )}
  </ul>
)
Is this good practice these days? I've spent the last X years trying to avoid inline JavaScript on elements.

Should doing something like documented on DOM Event Listeners be better? This has an example of binding an event to the window, but that seems bad too. Does the onClick binding on a component transpile into something else?

Can I bind to the Component or element inside componentDidMount instead of window?

code:
  componentDidMount: function() {
    window.addEventListener('click', this.handleClick);
  },
  handleClick: function(e) {
    e.preventDefault();
    // Do stuff
  }

You don't want to have inline click handlers ever on your components. What happens is that on each re-render you will generate a new function which causes downstream children to re-render. It's also a bit thrashy generating new functions like this all the time.

In an ideal world you would split off the individual child component and have a handler that you can call directly.

code:
class TodoItem extends React.Component {
  onClickHandler = (e) => {
    e.preventDefault();
    this.props.onTodoClick(this.props.todoId);
  }

  render() {
     <Todo onClick={this.onClickHandler}.../>
  }
}

const TodoList = ({ todos, onTodoClick }) => (
  <ul>
    {todos.map(todo =>
      <TodoItem
        key={todo.id}
        {...todo}
        onTodoClick={onTodoClick}
        todoId={todo.id}
      />
    )}
  </ul>
)

  • 1
  • 2
  • 3
  • 4
  • 5
  • Post
  • Reply