logo
Tags down

shadow

Reactjs Map Changes state of Props


By : manalalfaluoji alfal
Date : October 14 2020, 02:21 PM
around this issue If you are sure you want to render only positiveBlogs, you can do it like this:
this.props.positiveBlogs && this.props.positiveBlogs.map(...
code :


Share : facebook icon twitter icon

ReactJS: Props or State?


By : h0wReYa
Date : March 29 2020, 07:55 AM
will be helpful for those in need Its not a correct way to mutate the props like this.props.action.name=this.refs.newName.value;, The ideal way would be pass on a function from the parent component to the child component and call it from the child component which sets a value in the parent and then passes on to child
Also React DOCS recommend to make use of callback approach for refs rather than the string approach
code :
class App extends React.Component {
    constructor(props){
        super(props);
        actions=[];
    //parsing actions from localStorage
    }   
    changeListName = (val) => {
            /// take action here
    }
    render() {
        return(
          <GroupOfActions actions={actions} changeListName = {(val) => {this.changeListName}}/>
        );
    }
}


class GroupOfActions extends React.Component {   
    render (){
        var rows=[];
        var lastList=null;
        this.props.actions.forEach((action) =>{
            if (action.list!==lastList){
                rows.push(<ListOfActionsRow list={action.list} key={action.list} color={action.color} changeListName={(val) => this.props.changeListName(val)}/>);
            }
            rows.push(<ActionRow action={action} color={action.color} list={action.list}  key={action.name} />);

            lastList=action.list;
        });

        return(
        <div className="container" >
        {rows}          
        </div>
        );
    }
}

class ListOfActionsRow extends React.Component {
    onNameOfListChange() {
        var oldNameOfList=this.props.list;
        var newNameOfList=this.refs.newNameOfList.value;
        var toDoList=JSON.stringify(actions);
        var newToDoList;
        for (var i=0;i<toDoList.length;i++) {
            var nameIndex=toDoList.indexOf(oldNameOfList,i);
            if (nameIndex==-1) continue;
            i=nameIndex;
            var nameEndIndex=nameIndex+oldNameOfList.length;
            newToDoList=toDoList.substring(0,nameIndex)
                    +newNameOfList
                    +toDoList.substring(nameEndIndex);
        } 

        console.log(newToDoList);
        localStorage.setItem('toDoList',newToDoList);
        actions=JSON.parse(localStorage.getItem('toDoList'));
        console.log(actions);
        this.props.changeListName(this.newNameOfList.value);

    }

    render() {
        return (
        <div>
           <div className={'row '+(this.state.editionOfList?'hidden':'')} >
              <div className="col-md-6" onClick={this.editListRow} >
                 <div className="well well-small lead" style={{backgroundColor:'{this.props.color}'}} style={{marginTop:'20px'}} ref={(list) => {this.oldNameOfList}}>
                    {this.props.list}
                 </div>
              </div>
           </div>
           <div className={'row '+(this.state.editionOfList?'':'hidden')} >
           <div className="col-md-6" >
              <div className="well well-small lead" onClick={this.editListRow} style={{backgroundColor:'#fafafa'}}>
                 <input type='text' ref={(list) => {this.newNameOfList}} defaultValue={this.props.list} onChange={this.onNameOfListChange} />
                        <label>Color</label>
                        <select>
                            <option value="0">Yellow</option>
                            <option value="1">Blue</option>
                            <option value="2">Green</option>
                        </select>
              </div>
            </div>
           </div>
           <div className="row" style={{marginBottom:'20px'}}>
               <div className="col-md-2">
           </div>
        </div>
        );
    }
}

failed to map state to props in reactjs


By : Faris Karcic
Date : March 29 2020, 07:55 AM
I hope this helps you . props.userLoginStatus is not used correctly since, props here refer to the router props and not the PrivateRoute props, you can actually destructure the userLoginStatus prop
code :
import React from 'react';
import { connect } from 'react-redux'; 
import { Redirect,Route} from 'react-router-dom';

const PrivateRoute = ({ component: Component, userLoginStatus, ...rest }) => (
  <Route {...rest} render={(props) => (
   userLoginStatus==1
      ? <Redirect to='/home' />
      : <Component {...props} />
  )} />
)
const mapStateProps=(state)=> {
    console.log(state.loginDetails.status);
    return {
        userLoginStatus:state.loginDetails.status
    }
}
export default connect(mapStateProps)(PrivateRoute);

ReactJS: State does not update upon onChange when initial state is from props


By : user2721571
Date : March 29 2020, 07:55 AM
it fixes the issue You are saying [e.target.name]: e.target.value, or name = value but you are not sending name field.
This code should fix the issue.
code :
<Form.Input 
label="First Name"
name='firstName'
value={this.state.firstName}
placeholder="Jose"
onChange={this.handleChange}
/>
  constructor(props) {
    super(props)
    this.handleChange = this.handleChange.bind(this)
  }

reactjs props VS state: not setting state right


By : user3312732
Date : March 29 2020, 07:55 AM
To fix this issue Because you're using a class you need to bind your function so it has correct this
next={this.fetchMoreData.bind(this)}

How does props and state work in REACTjs?


By : user3635951
Date : March 29 2020, 07:55 AM
I wish this help you Simple state is specific to single component. you can define properties within the state. Once you change the state by setstate page will render again. example for props is below.
code :
export default class Devices extends Component {
 constructor(props) {
    super(props);
    this.state = {
      userName: "Name 1",
      itemCount: 0, // this property can use within the Devices Component not in Device detail
     }
  }

  render() {
     .....
     //you can pass username to DeviceDetail page 
     <div>
          <DeviceDetail userName={this.state.userName} />
     </div>
}
}

//Inside DeviceDetail class
export default class DeviceDetail extends Component {

    constructor(props) {
        super(props);
        this.state = {
            userName: props.userName, //you can call username using props
        }
    }
}


  getAllDevices = () => {
    var totalDevicesCount = 0; //  property is initially in the global scope. 

    let apiUrl = ....URL
    var response = fetch(apiUrl);
    const data = response.json();
    totalDevicesCount = data.length;

    if (data.length > 0) {
      this.state.itemCount = totalDevicesCount; // Since this is Var can use inside different block as well but inside this if condition you cannot use apiUrl. because it's let
    }

  }

Related Posts Related Posts :
  • How to resolve ':react-native-vector-icons' error for a successful build?
  • React 16.6.3 Context API contextType not being set
  • react fetch data and render the error if any
  • Setting Relative Path in React Router
  • Jest: How to provide Redux mock store to only the child component?
  • How to solve selectOptions.some is not a function while using AsyncCreatableSelect?
  • Modify css react component with styled-component
  • how to use npm run build on your react app?
  • How to automatically create a Sentry release and upload source-maps to Sentry in a react project?
  • Returning const with fragment to
  • disable check on a row click in DetailsList
  • Best way to update component after this handleClick?
  • mobx-react: component no response to injected store
  • when to and when not to use "this" in JSX event handler function reference?
  • React: how to forward refs to multiple children?
  • webpack errors out when using -p flag
  • Looping Lists with onClick Doesn't Work React
  • React - Jest - Enzyme: How to mock ref properties
  • Mock-axios-adapter not mocking get request
  • Map function React
  • How to retrieve data from promise object in React JS
  • Eslint-plugin-react configuration docs
  • Wordpress Gutenberg React – render variable with HTML
  • shadow
    Privacy Policy - Terms - Contact Us © voile276.org