javascript

Can't access nested object properties in a single object from JSON call in REACT

邮差的信 提交于 2021-02-20 19:15:49
问题 I'm making an Rails API call and return a single JSON object that has a nested User Object and a tag list array. However, I can't access the nested Object. this.props.post.user.name throws: Cannot read property 'name' of undefined. I am confused because when I make the call to PostsIndex in PostsIndex.js and get an array of objects and map through it I can access everything. Is there something I need to do when only dealing with a single object? PostShow.js import React, {Component} from

Can't access nested object properties in a single object from JSON call in REACT

与世无争的帅哥 提交于 2021-02-20 19:15:22
问题 I'm making an Rails API call and return a single JSON object that has a nested User Object and a tag list array. However, I can't access the nested Object. this.props.post.user.name throws: Cannot read property 'name' of undefined. I am confused because when I make the call to PostsIndex in PostsIndex.js and get an array of objects and map through it I can access everything. Is there something I need to do when only dealing with a single object? PostShow.js import React, {Component} from

Make div expand to take all the available space [duplicate]

巧了我就是萌 提交于 2021-02-20 19:12:39
问题 This question already has answers here : Make a div fill the height of the remaining screen space (34 answers) Closed 3 years ago . I want a desktop-like full-page width layout. Some menu at the top (uknown height, depending on the content), and div underneath that takes ALL the available space in viewport. div { padding: 0px } html, body { height: 100%; padding: 0px; margin: 0px; } .outer { background: olive; height: 100%; } .menu { background: orange; } .should_fill_available_space {

Why not to use splice with spread operator to remove item from an array in react?

馋奶兔 提交于 2021-02-20 19:11:45
问题 splice() mutates the original array and should be avoided. Instead, one good option is to use filter() which creates a new array so does not mutates the state. But I used to remove items from an array using splice() with spread operator. removeItem = index => { const items = [...this.state.items]; items.splice(index, 1); this.setState({ items }); } So in this case when I log items changes but this.state.items stays unchanged. Question is, why does everyone use filter instead of splice with

Why not to use splice with spread operator to remove item from an array in react?

心不动则不痛 提交于 2021-02-20 19:11:20
问题 splice() mutates the original array and should be avoided. Instead, one good option is to use filter() which creates a new array so does not mutates the state. But I used to remove items from an array using splice() with spread operator. removeItem = index => { const items = [...this.state.items]; items.splice(index, 1); this.setState({ items }); } So in this case when I log items changes but this.state.items stays unchanged. Question is, why does everyone use filter instead of splice with

Call a method passed as a prop in react

a 夏天 提交于 2021-02-20 19:10:12
问题 i have method in my parent element, an d i passed it as a prop; like this: <NavBar retrieveList={this.retrieveList}/> and in my child component i can not able to call this method from another method body. handleCloseModal () { window.$('#newHireModal').modal('hide'); /* this is working */ console.log(this.props.retrieveList); /* it gives method body, just to be sure props is coming here */ this.props.retrieveList; /*it gives "Expected an assignment or function call..." */ this.props

Could not find property of window when doing JS interop with Blazor

不问归期 提交于 2021-02-20 19:09:34
问题 Hello i am trying to call a method from a js file from Blazor . My file structure is like this: -root -JSInterop.cs -js(folder) -meth.js (file containing the js method) I keep getting the following error : Could not find 'methods' in 'window'. **Cs class that calls the js ** public class JSInterop { public static async Task<string> ChangeText() { try { var data = await JSRuntime.Current.InvokeAsync<string>("./js/meth/methods.print","mymessage"); Console.WriteLine($"ReturnedFromJS:{data}");

How to stop re render child component when any state changed in react js?

為{幸葍}努か 提交于 2021-02-20 19:06:31
问题 I have a parent componenet, Inside this I have included a child component with props. but When any state is changed in parent component (that doesn't related to child component) then child component re-render. I don't want this re render on every state change. Can we stop? import React, { useState } from "react"; import ReactDOM from "react-dom"; import Child from "./child"; import "./styles.css"; function App() { const [any_state, setAnyState] = useState(false); const handleClick = () => {

Trigger mouse click and random intervals

佐手、 提交于 2021-02-20 19:05:11
问题 I am trying to automate a task where I have to continuously do left mouse click on an hand icon. I am able to do that on a set time frame, for example 32 sec and 756 ms but I need to do this with a random timeframe. For example if we can add 2-3 seconds after each left mouse click. Can anyone guide me how to make the click interval random? I am using Chrome. setInterval(function(){ $( "[id^='hand_'].handIcon").trigger('click'); }, 32756); 回答1: Even if you use a Math.random at the setInterval,

React - react-scripts publicPath

时间秒杀一切 提交于 2021-02-20 19:02:10
问题 Is there possibility to override publicPath in react-scripts for dev env. I use symfony and I include react app inside twig so I had to change assets to serve from http://localhost:3000/static/js/bundle.js - this works fine but I have problem with static files because they are rendered in browser as '/static/media/logo.813ua.png' and my current url is http://localhost:8000 What I did is I run yarn eject and modify in webpack.config.dev.js: var publicPath = 'http://localhost:3000/' and