score:2
because of the variable scoping on javascript. since you're inside a function there, like that:
success: function(data) {
this.setstate({data: data});
}
your "this
" will refer to the scope inside the success function. when you do a .bind(this)
after the function, it tells the js that you want to use the outer reference for this. in your case, the scoping for the outer function "loadcommentsfromserver
"
also i'd recomend you further reading: http://javascriptissexy.com/javascript-apply-call-and-bind-methods-are-essential-for-javascript-professionals/
http://www.smashingmagazine.com/2014/01/23/understanding-javascript-function-prototype-bind/
score:1
the bind method allows you to change the context of a function.
here is a little example to understand bind()
var mypie = {
love : 'full'
};
function doilove(){
console.log(this.love);
}
doilove(); // returns undefined
doilove().bind(mypie); // returns 'full'
actually, your code is to call the this.props.url
url, we expect json
data from the server, if we success we execute the success
function, else if we failed we execute the error
function.
Source: stackoverflow.com
Related Query
- ReactJS - Using jQuery to load data from a server
- ReactJS not fetching data from server when using 'url' attribute
- how to fetch the data from server like given in console in reactjs using axios
- I can't get the data from my server using reactjs and express
- Receiving data from server using reactjs
- how to hide the Row if there is no data fetched from backend server using axios in reactjs
- Send IP address from frontend using ReactJS to server NodeJS
- Reactjs - getting data from server and updating
- How to pass data from one component to another while using API in reactjs
- How to retrieve data from the server using fetch get method and show it in a table
- How to pass data in server side rendering to reactjs component from node
- Data from Parent to child using props in ReactJs is not working properly
- Load ReactJS App from another server and execute
- Using data from one component in another in Reactjs
- ReactJS - Proper way to fetch data from redux and load form
- Where should I load data from server in Redux + ReactJS?
- How to access data from get request made by a React functional component to localhost server using Axios?
- How to fetch data from json using Reactjs
- Display data from an API when clicking a button. Using ReactJS
- How to GET limited data from server using axios?
- Bug getting data from server to client using MERN stack
- In React is it possible to avoid reloading data from server upon pressing browser back button (without using Redux)?
- Rendering ReactJS from asynchronous server side data calls
- How to retrieve images from server path using reactjs
- Sending data from the client to the server and displaying to the terminal using `sockets`
- I'm trying to fetch data from a server using useEffect react hook
- For some reason, cant get the data from an api to render on the screen using axios in Reactjs
- Rendering correct data from json api using ReactJS
- Reactjs initial ajax call from initial page load using chrome extremely slow
- How to delete data from server and UI using React and Node?
More Query from same tag
- save the previous state to one array in ReactJS
- Specialized shouldComponentUpdate on PureComponent
- How to getById in redux
- Redux reducer not updating prop value in React component from dispatch outside React components
- React-router re-render on URL change with route using regex
- react hooks with redux, state not updating
- what is the difference between import Task and import { Task } in es6
- Enzyme's simulate() not changing output for onChange event
- how to get an index from array onClick
- How to hide navbar in login or signup page reactn router
- Not able to get a fielding data which is in json format coming from api in react js
- How to rerender functional component on state change?
- Next.js : Refresh page after modifying a file, without rerunning 'npm run'
- @media rule not turning off when screen gets larger
- Why is export from index.tsx file undefined? (React + Typescript)
- Get data from a local API with Redux
- Why is my redux state not updating after dispatch
- How to hide a button unless a variable has a value
- How to add custom `PNG` image to material ui icon
- Redirect to url on commponentDidMount in App.js
- React Smooth DND Library causing and 'illegal invocation error'
- [reactjs]Anonymous function definitions not equivalent
- Loop through objects containing array of objects in reactjs
- React: Making width of div equal to height. The height is not being calculated correctly
- Styled components styles overridden when using props
- React.js export async function
- how to add html and css in my home page which is written in nextjs
- How to add history in my this.props? For push to link
- react typescript : usestate type
- When to use componentWillReceiveProps lifecycle method?