React JS Multiple Choice Questions – Part 2

This is the part-2 of React JS Multiple Choice Questions (MCQs). Please find below URL for Part-1 MCQs on React JS:-

React JS MCQs – Part 1 

  Q1) Which of the following lifecycle methods does not get triggered on the component’s initial render?
a) componentWillMount()
b) componentWillReceiveProps()
c) render()
d) componentDidMount()
  Q2) Given the below class component: –
 class Hello extends React.Component {
render() {
return <div>{this.props.myProp}</div>;
}
}
Which code snippet below declares “newValue” as the default prop value for this.props.myProp?
a) this.setState({ myProp: “newValue” });
b) Hello.props.default = { myProp: ‘newValue’ };
c) Hello.myProp = “newValue”;
d) this.myProp = “newValue”;
e) Hello.defaultProps = { myProp: ‘newValue’ };
  Q3) How do you stop event propagation in React?
a) e.false()
b) return true
c) return false
d) e.stopPropagation()
e) e.preventPropagation()
  Q4) How do you define a React pure component?
a) extends React.PureComponent
b) All function components are Pure Components
c) class PureComponent {}
d) extends React.Component(Pure)
  Q5) Mixins are not available when defining components with JavaScript classes. What’s the recommended alternative?
a) Modules
b) JavaScript multiple inheritance
c) Higher Order Components
d) Decorators
  Q6) Which lifecycle methods are not triggered if shouldComponentUpdate() returns false?
a) componentWillMount(), render(), and componentDidMount()
b) componentWillReceiveProps() and render()
c) render()
d) render(), and componentDidUpdate()
  Q7) What is shallow rendering?
a) Rendering the component without needing to pass any props or setting any state.
b) Rendering just the component and not its children.
c) Rendering the component with a shallow context object.
d) Rendering the component without enforcing the prop types.
  Q8) Will any lifecycle methods be triggered if the “props” of a component change?
a) Yes, but only if props were changed from within the component
b) No, props cannot be changed
c) No, only state changes trigger update lifecycle methods
d) Yes, both props and state change triggers update lifecycle methods
  Q9) What is the way to avoid passing props through intermediate elements in the component tree?
a) React.fragment();
b) React.Component();
c) React.createContext();
d) React.createRef();
  Q10) Which of the following is NOT a good reason for having presentational and container logic separate in a component?
a) Shorter code overall
b) Separating responsibilities
c) Readability
d) Testability
  Q11) Why use async/await over simple promise chains?
a) They are faster.
b) Readability
c) They are required in modern JavaScript.
d) You cannot alter a components state without them.
  Q12) What is wrong with the key attribute in the below code: –

function ListItem({ value }) {
return <li key={value}>{value}</li>;
}
function NumberedList(props) {
return (
<ol>
{props.numbers.map(number => <ListItem value={number} />)}
</ol>
);
}

a) This code will cause a syntax error because value is a reserved property.
b) The key attribute cannot be used on HTML elements like <li>.
c) The key attribute should be used on <ListItem /> instead of <li>.
d) A number cannot be used as a key.

ANSWERS

1 B
2 E
3 D
4 A
5 C
6 D
7 B
8 D
9 C
10 A
11 B
12 C

Set Lookup field in another site collection using REST API

In this article, I will explain how you can set a lookup field in another list in different site collection using REST API.

You need to get other site collection form digest value and pass it to the X-RequestDigest in the headers. You can get this value by making a POST request to http://<server url>/_api/contextinfo and then use d.GetContextWebInformation.FormDigestValue as the form digest value while making the call.

Note that you need to specify method:”POST” instead of type:”POST” for the REST API call to create the item.

Set Lookup field using REST API in SharePoint

In this article, I will explain how you can set value in a Lookup field in a SharePoint List using REST API. I have tested the below code in SharePoint 2013/2016 and in SharePoint Online.

Scenario:-

List Name – MasterCountry
Lookup Column – Region
Parent List Name – MasterRegion

You need to pass the ID value (idToGet in below code) from the parent list. For that, pass the column name concatenated with Id – “RegionId”

No need to pass any select or expand parameter in REST API URL in POST operation.