Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

can you provide a syntactic sugar? #122

Open
zhangenming opened this issue Sep 10, 2019 · 3 comments
Open

can you provide a syntactic sugar? #122

zhangenming opened this issue Sep 10, 2019 · 3 comments

Comments

@zhangenming
Copy link

zhangenming commented Sep 10, 2019

function Father() {
  const [state, setState] = useState(false)
  function toggle() {
    setState(!state)
  }
  return (
    <div>
      <button onClick={toggle}>Father</button>
      {state ? <Child func={toggle} /> : null}
    </div>
  )
}
function Child({ func }) {
  return (
    <div onClick={func}>
      <button>Child</button>
    </div>
  )
}

in this case, we just want bind the click event to the child root
so, should there has a syntactic sugar like this?

// diff Father code
function Father() {
      {state ? <Child onClick={toggle} /> : null}
       //in Father compoent, Think like, we just immediate bind the click event at here
}

function Child() {
  return (
    <div class='root'> //  so  here , root node, we can omit 'onClick={func}'
      <button>child</button>
    </div>
  )
}

thanks

@yuanoook
Copy link

onClick is just a prop for a component; the component itself decides how to use the props.

For a dom node like <button>, onClick is a builtin prop, and will trigger when the dom is clicked;
But for a composite component like <Child> here, onClick is just a normal prop, and the code inside should tell how to use it, when to call it.

Passing props cross components by syntactic sugar will make components very hard to manage and confusing.

@zhangenming
Copy link
Author

zhangenming commented Sep 16, 2019

but, in this particular case, that we just want bind the event to the root node
we immediate bind the event on the parent node(Father) will be more clearer?
and we cant provide a name , like react-click ( <Child react-click={toggle} /> ) or whatever(reference #66)

function Father() {
      {state ? <Child onClick={toggle} /> : null}
}

function Child() {
  return (
    <div class='root'> //  so  here , at root node, we can omit 'onClick={func}'
      <button>child</button>
    </div>
  )
}

@nortonwong
Copy link

The Child must determine which node accepts click events. Suppose it renders a wrapper node that holds a primary button and other nodes. The outer node should not intercept the prop.

const Child = ({ onClick }) => (
  <div style={...}>
    <button onClick={onClick} />
    <button onClick={...} />
    <button onClick={...} />
  </div>
);

The Child should also be able to reject event handlers based on internal logic.

const Child = ({ onClick }) => {
  const [isBusy, setBusy] = useState(...);
  return <div onClick={isBusy ? undefined : onClick} />;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants