How do I conditionally add attributes to React components?

Apparently, for certain attributes, React is intelligent enough to omit the attribute if the value you pass to it is not truthy. For example: const InputComponent = function() { const required = true; const disabled = false; return ( <input type=”text” disabled={disabled} required={required} /> ); } will result in: <input type=”text” required> Update: if anyone … Read more

What is the difference between React Native and React?

ReactJS is a JavaScript library, supporting both front-end web and being run on a server, for building user interfaces and web applications. It follows the concept of reusable components. React Native is a mobile framework that makes use of the JavaScript engine available on the host, allowing you to build mobile applications for different platforms … Read more

Understanding unique keys for array children in React.js

You should add a key to each child as well as each element inside children. This way React can handle the minimal DOM change. In your code, each <TableRowItem key={item.id} data={item} columns={columnNames}/> is trying to render some children inside them without a key. Check this example. Try removing the key={i} from the <b></b> element inside … Read more

React-router URLs don’t work when refreshing or writing manually

Server-side vs Client-side The first big thing to understand about this is that there are now 2 places where the URL is interpreted, whereas there used to be only 1 in ‘the old days’. In the past, when life was simple, some user sent a request for http://example.com/about to the server, which inspected the path … Read more

Why use Redux over Facebook Flux? [closed]

Redux author here! Redux is not that different from Flux. Overall it has same architecture, but Redux is able to cut some complexity corners by using functional composition where Flux uses callback registration. There is not a fundamental difference in Redux, but I find it makes certain abstractions easier, or at least possible to implement, … Read more

How to pass props to {this.props.children}

Cloning children with new props You can use React.Children to iterate over the children, and then clone each element with new props (shallow merged) using React.cloneElement. For example: const Child = ({ doSomething, value }) => ( <button onClick={() => doSomething(value)}>Click Me</button> ); function Parent({ children }) { function doSomething(value) { console.log(“doSomething called by child … Read more

What are these three dots in React doing?

That’s property spread notation. It was added in ES2018 (spread for arrays/iterables was earlier, ES2015), but it’s been supported in React projects for a long time via transpilation (as “JSX spread attributes” even though you could do it elsewhere, too, not just attributes). {…this.props} spreads out the “own” enumerable properties in props as discrete properties … Read more

Loop inside React JSX

Think of it like you’re just calling JavaScript functions. You can’t use a for loop where the arguments to a function call would go: return tbody( for (let i = 0; i < numrows; i++) { ObjectRow() } ) See how the function tbody is being passed a for loop as an argument – leading … Read more

Programmatically navigate using React router

React Router v5.1.0 with hooks There is a new useHistory hook in React Router >5.1.0 if you are using React >16.8.0 and functional components. import { useHistory } from “react-router-dom”; function HomeButton() { const history = useHistory(); function handleClick() { history.push(“/home”); } return ( <button type=”button” onClick={handleClick}> Go home </button> ); } React Router v4 … Read more