score:385
The @
symbol is in fact a JavaScript expression currently proposed to signify decorators:
Decorators make it possible to annotate and modify classes and properties at design time.
Here's an example of setting up Redux without and with a decorator:
Without a decorator
import React from 'react';
import * as actionCreators from './actionCreators';
import { bindActionCreators } from 'redux';
import { connect } from 'react-redux';
function mapStateToProps(state) {
return { todos: state.todos };
}
function mapDispatchToProps(dispatch) {
return { actions: bindActionCreators(actionCreators, dispatch) };
}
class MyApp extends React.Component {
// ...define your main app here
}
export default connect(mapStateToProps, mapDispatchToProps)(MyApp);
Using a decorator
import React from 'react';
import * as actionCreators from './actionCreators';
import { bindActionCreators } from 'redux';
import { connect } from 'react-redux';
function mapStateToProps(state) {
return { todos: state.todos };
}
function mapDispatchToProps(dispatch) {
return { actions: bindActionCreators(actionCreators, dispatch) };
}
@connect(mapStateToProps, mapDispatchToProps)
export default class MyApp extends React.Component {
// ...define your main app here
}
Both examples above are equivalent, it's just a matter of preference. Also, the decorator syntax isn't built into any Javascript runtimes yet, and is still experimental and subject to change. If you want to use it, it is available using Babel.
score:51
Very Important!
These props are called state props and they are different from normal props, any change to your component state props will trigger the component render method again and again even if you don't use these props so for Performance Reasons try to bind to your component only the state props that you need inside your component and if you use a sub props only bind these props.
example: lets say inside your component you only need two props:
- the last message
- the user name
don't do this
@connect(state => ({
user: state.user,
messages: state.messages
}))
do this
@connect(state => ({
user_name: state.user.name,
last_message: state.messages[state.messages.length-1]
}))
Source: stackoverflow.com
Related Query
- React + Redux - Why not connect all the components?
- How to use currying to create HoC in React and connect to the Redux store at the same time?
- How should I connect child components of a reduxForm to the redux state?
- How to fix Uncaught TypeError: Object(...) is not a function error when I connect Redux to the project
- connect multiple reducers using decorator in redux
- How can i connect redux with localStorage so that when updating the like on the page does not disappear
- Whats the right way to redirect when data is not found with Redux and Router?
- Redux: Strange destructuring syntax inside the Redux docs example's connect method?
- Redux connect works but now for all properties from the state
- Typings for redux / connect like decorator
- How can I use a ref callback function on a component this is connect to the redux store?
- How do we connect the redux store to a file that has only an array of objects?
- What is the best way to access redux store outside a react component?
- What could be the downsides of using Redux instead of Flux
- What's the '@' (at symbol) in the Redux @connect decorator?
- React + Redux - What's the best way to handle CRUD in a form component?
- While debugging, can I have access to the Redux store from the browser console?
- How to show a loading indicator in React Redux app while fetching the data?
- Whats the best way to update an object in an array in ReactJS?
- Getting an error "A non-serializable value was detected in the state" when using redux toolkit - but NOT with normal redux
- What does the at symbol (@) do in ES6 javascript? (ECMAScript 2015)
- I am using Redux. Should I manage controlled input state in the Redux store or use setState at the component level?
- React + Redux - Input onChange is very slow when typing in when the input have a value from the state
- How to put methods onto the objects in Redux state?
- having multiple instance of same reusable redux react components on the same page/route
- What is the core difference of redux & reflux in using react based application?
- Can I view/modify the Redux store using Chrome Dev Tools
- React js - What is the difference betwen HOC and decorator
- How to divide the logic between Redux reducers and action creators?
- What is the difference between Redux Thunk and Redux Saga?
More Query from same tag
- How to set value for KeyboardDatePicker?
- Reactjs material icons in css
- Is there an alternative to Object.assign performance wise
- reactjs/react-router-dom: Browserrouter routing question
- React JS: Unexpected token < in JSON at position 0
- firebase deployment issue getting a page from firebase telling me firebase hosting setup is complete when i should get my website. react app
- Why isnt my Netlify Contact Form working?
- How to dynamically change the title of a website in react js (also in source code)?
- ReactJS Preload Navigation Elements
- Can we use functional component and react hook with ag-grid table?
- Opacity transition affecting child with backdrop-filter
- Heroku Error: Module not found: Error: Can't resolve 'React' in '.../src/main/js/components
- Pokedex project api react js
- React.js -- How to add an animation when unmounting a component and mounting another?
- How do we read one level nested deep array data from firebase in react js?
- How can I fix my code to display correct index of array
- How to inline/include css in a react library package
- Need help to get started with React
- Using css modules, how can I use composes to include a list of variables for colors
- Material UI SpeedDial | How to customize ToolTips
- Loading data into component based on route in react
- Redirect all routes in react router
- .env variable returns undefined in React JS app
- How to get specific value from react Component
- React-redux get site base URL / window.location
- history.push() does not updates browser's url
- Function in a text field onChange doesnt get called
- How can I to display photo in modal?
- I want to create a button inside a cell of react-table column
- Simple logout component throws "Cannot update a component from inside the function body of a different component"