Asked  7 Months ago    Answers:  5   Viewed   24 times

My structure looks as follows:

Component 1

 - |- Component 2

 - - |- Component 4

 - - -  |- Component 5

Component 3

Component 3 should display some data depending on state of Component 5.

Since props are immutable, I can't simply save its state in Component 1 and forward it, right? And yes, I've read about Redux, but I don't want to use it. I hope that it's possible to solve it just with react. Am I wrong?



For child-parent communication you should pass a function setting the state from parent to child, like this

class Parent extends React.Component {
  constructor(props) {

    this.handler = this.handler.bind(this)

  handler() {
      someVar: 'some value'

  render() {
    return <Child handler = {this.handler} />

class Child extends React.Component {
  render() {
    return <Button onClick = {this.props.handler}/ >

This way the child can update the parent's state with the call of a function passed with props.

But you will have to rethink your components' structure, because as I understand components 5 and 3 are not related.

One possible solution is to wrap them in a higher level component which will contain the state of both component 1 and 3. This component will set the lower level state through props.

Tuesday, June 1, 2021
answered 7 Months ago

There's no need to bind functions/callbacks in functional components since there's no this in functions. In classes, it was important to bind this because we want to ensure that the this in the callbacks referred to the component's instance itself. However, doing .bind in the constructor has another useful property of creating the functions once during the entire lifecycle of the component and a new callback wasn't created in every call of render(). To do only initialize the callback once using React hooks, you would use useCallback.


class Foo extends Component {
  constructor(props) {
    this.handleClick = this.handleClick.bind(this);

  handleClick() {
    console.log('Click happened');

  render() {
    return <Button onClick={this.handleClick}>Click Me</Button>;


function Foo() {
  const memoizedHandleClick = useCallback(
    () => {
      console.log('Click happened');
    [], // Tells React to memoize regardless of arguments.
  return <Button onClick={memoizedHandleClick}>Click Me</Button>;
Monday, August 2, 2021
answered 5 Months ago

please consider put below config on your .babelrc

    "presets": ["react", "es2015", "stage-0"]

it should work. also i see that you have nested array inside "presets". every preset should be one of presets elements.

and i'm strongly recommend that you use latest babel(version 7). when you upgrade to babel 7 you should download @babel/preset-react and @babel/preset-env and that should be enough. and .babelrc will look like this:

  "presets": [
Friday, August 6, 2021
answered 4 Months ago

React components expose all the standard Javascript mouse events in their top-level interface. Of course, you can still use :hover in your CSS, and that may be adequate for some of your needs, but for the more advanced behaviors triggered by a hover you'll need to use the Javascript. So to manage hover interactions, you'll want to use onMouseEnter and onMouseLeave. You then attach them to handlers in your component like so:

    onMouseEnter={() => this.someHandler}
    onMouseLeave={() => this.someOtherHandler}

You'll then use some combination of state/props to pass changed state or properties down to your child React components.

Sunday, August 8, 2021
answered 4 Months ago

I recommend initializing your socket connection in CategoriesList and then passing down the connection as props to the child components. When the connection is passed down, you should be able to use it to listen for specific events as needed in the child components.

Here's an example application on github that uses react and The socket is initialized in a parent component and then passed down.

On line 9 the connection is initialized and then on line 23 it's passed down as props. The connection is later used in child components to receive and emit events. Ex:

Saturday, October 9, 2021
answered 2 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :