Using the mern.io scaffolder Tool - What is the .need Method?

Based on scaffolder mern.io I was looking through the code to find out what was going on. I came across a .need method, which looks like something related to es6 classes. I cannot find any useful information anywhere, so I ask what is a method .need?

class PostContainer extends Component {
   //do class setup stuff here
}

PostContainer.need = [() => { return Actions.fetchPosts(); }];

You can easily and easily launch a project using these commands.

npm install -g mern-cli
mern YourAppName
+4
source share
1 answer

The mern documentation is pretty short when it comes to explaining this.

fetchComponentData ( , ) . , .

- , .

, .

posts Redux prop posts, .

// PostContainer.jsx
function mapStateToProps(store) {
  return {
    posts: store.posts,
  };
}

, API.

// reducer.js
// initial state of the store is an empty array
const initialState = { posts: [], selectedPost: null };

, , , Actions.fetchPosts().

// actions.js
export function fetchPosts() {
  return (dispatch) => {
    return fetch(`${baseURL}/api/getPosts`).
      then((response) => response.json()).
      then((response) => dispatch(addPosts(response.posts)));
  };
}

, .

Caveat

React. , mern , fetchComponentData, , Redux .

// server.js
fetchComponentData(store.dispatch, renderProps.components, renderProps.params)

, needs . "" promises.

// fetchData.js
const promises = needs.map(need => dispatch(need(params)));
return Promise.all(promises);

, Promise.all(promise) , Redux, .

, , ES6, .

ES6 , , , .

needs , promises fetchComponentData. , . , .

const fetchPosts = () => { return Actions.fetchPosts() };
const needs = [fetchPosts];
PostContainer.need = needs;
+6

All Articles