react.js – A todo application in React using hooks

I am learning React and wanted to implement a todo application using functional components. Any suggestions and criticisms are welcome, especially with regard to best practices.

import React, {useState} from 'react';

class Todo {
    constructor(id, description, complete) {
        this.id = id;
        this.description = description;
        this.complete = complete;
    }
}

export default () => {

    const (idCounter, setIdCounter) = useState(1);
    const (todoList, setTodoList) = useState(());
    const (todoDescription, setTodoDescription) = useState('');

    function addTodo() {
        setTodoList(
            (...todoList, new Todo(idCounter, todoDescription, false))
        );
        setIdCounter(idCounter + 1);
        setTodoDescription('');
    }

    function markTodoItemComplete(id) {
        const todoListClone = todoList.slice();
        todoListClone(id - 1).complete = true;
        setTodoList(todoListClone);
    }

    return <>
        {todoList.map((todoItem) => {
            return (
                
                    {todoItem.id} - {todoItem.description} - {todoItem.complete ? 'Completed' : 'Waiting'}
                    
                    
); })}
setTodoDescription(e.target.value)} value={todoDescription} />
}

enter description of image here

javascript – correct coding structure in React.js?

Lifecycle hooks are functions that are called at different stages of a component. Here constructor() and componentDidMount() works differently. From my point of view, state variables are initialized inside constructor() and API calls are made in componentDidMount() hook. we are not supposed to define definitions of functions within the life cycle brackets. Your above component can be converted to

class Demo extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      isActive: false
    };
  }

  setToActive = (e, id) => {
    const {
      setTaskID,
      arrowStyleToDone,
      closeAllTasks,
      closeAllDoing,
      disableArrowButton
    } = this.props;
    setTaskID(id);
    arrowStyleToDone();
    if (!e.target.classList.contains("active")) {
      e.currentTarget.classList.add("active");
      e.currentTarget.classList.add("border-done");
      closeAllTasks();
      closeAllDoing();
    } else {
      e.currentTarget.classList.remove("active");
      e.currentTarget.classList.remove("border-done");
      disableArrowButton();
    }
  };

  componentDidMount() {} // no need for this lifecycle since we are not making any initial function call, like fetch from api etc.

  render() {
    const { done } = this.props;
    const { isActive } = this.state;
    return (
      
Done {done && done.map(item => (
this.setToActive(e, item.id)} >
))}
); } }

The basic structure of the React component is

Class component

class Class_Name extends Component{
    constructor(){
      this.state = {} // state variable
      this.toggleTab = this.toggleTab.bind(this);
    }

   // other functions
   toggleTab = ()=>{} // these are example functions.

   // life cycle hooks if you are using any

   render(){
    return (

    )

   }


}

reactjs – state value is always default. React js 16.12.0

I have two useEffect-s. One is used to retrieve data from the API and save it in the state and the second is called only once and it starts listening for the Websocket event.
In the websocket event handler, I save the extracted data, but it is always the default.
Even if the data recovery completes successfully and the list is drawn on the user interface, the value of the list is still empty – ().

const (list, setList) = useState(());

useEffect(() => {
   axios.get("https://sample.api.com/get/list")
       .then(res => {
           setList(res.data);
       });
}, (window.location.pathname.split("https://stackoverflow.com/")(2)));

useEffect(() => {
   webSocket.on('messageRecieved', (message) => {
        console.log(list);
    }); 
}, ());

reactjs – MobX. @observable works but @computed does not work

I have the following code:
It works well if _isFavorite the variable is public observable. But once I change it to @computed, it only fires once. But further @action calls do not trigger @computed.

It works well, once isFarovite is changed:

class Cat {
    @observable public isFavorite = false;

    constructor() { ... }

    @action public toggleFavorite() {
        this.isFavorite = !this.isFavorite;
    }
}

It does not work if _isFavorite is changed:

class Cat {
    private _isFavorite = false;

    constructor() { ... }

    @computed public get isFavorite(): boolean {
        return this._isFavorite;
    }

    @action public toggleFavorite() {
        this._isFavorite = !this._isFavorite;
    }
}

Am I doing everything right?

Get a great React JS website or get your money back for $ 120

Get an awesome React JS website or get your money back

I am a professional front end developer, currently employed in a web development company. I have worked for a renowned client in India and therefore I have gained enough experience in the field of front-end development. Here I aim to build you a basic React JS website in a quick span of time. Feel free to ask questions.

.

reactjs – npm start no longer works for create-react-app

It has been a frustrating problem that has arisen for a reason that does not seem clear to me. This has happened to me 3 times now, and the first 2 times I have just created a new React application, but I would prefer to find a solution for it.

I have referenced another package.json file from React applications and it is exactly the same, but this one is capable of running npm start.

{
  "name": "lyrics_app02",
  "version": "0.1.0",
  "private": true,
  "dependencies": {
    "@testing-library/jest-dom": "^4.2.4",
    "@testing-library/react": "^9.4.0",
    "@testing-library/user-event": "^7.2.1",
    "react": "^16.12.0",
    "react-dom": "^16.12.0",
    "react-router-dom": "^5.1.2",
    "react-scripts": "3.3.0"
  },
  "scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test",
    "eject": "react-scripts eject"
  },
  "eslintConfig": {
    "extends": "react-app"
  },
  "browserslist": {
    "production": [
      ">0.2%",
      "not dead",
      "not op_mini all"
    ],
    "development": [
      "last 1 chrome version",
      "last 1 firefox version",
      "last 1 safari version"
    ]
  }
}


Here is the error message I received.

0 info it worked if it ends with ok
1 verbose cli [
1 verbose cli   'C:\Program Files\nodejs\node.exe',
1 verbose cli   'C:\Program Files\nodejs\node_modules\npm\bin\npm-cli.js',
1 verbose cli   'start'
1 verbose cli ]
2 info using npm@6.12.1
3 info using node@v12.13.1
4 verbose stack Error: ENOENT: no such file or directory, open 'C:UsersRachaDownloadsJavascriptProjectsLyrics_App02package.json'
5 verbose cwd C:UsersRachaDownloadsJavascriptProjectsLyrics_App02
6 verbose Windows_NT 10.0.17763
7 verbose argv "C:\Program Files\nodejs\node.exe" "C:\Program Files\nodejs\node_modules\npm\bin\npm-cli.js" "start"
8 verbose node v12.13.1
9 verbose npm  v6.12.1
10 error code ENOENT
11 error syscall open
12 error path C:UsersRachaDownloadsJavascriptProjectsLyrics_App02package.json
13 error errno -4058
14 error enoent ENOENT: no such file or directory, open 'C:UsersRachaDownloadsJavascriptProjectsLyrics_App02package.json'
15 error enoent This is related to npm not being able to find a file.
16 verbose exit [ -4058, true ]


react.js – React – Use the default accessory when the accessory is required

My code reviewer tells me to use a default accessory when the accessory is required.

I'm not. If you use a default property on a required property, your property is simply no longer needed, because it will have a value whether you pass it or not.

What do you think

ProgressBar.propTypes = {
  progress: PropTypes.number.isRequired,
  progressBarColor: PropTypes.string
}

reactjs – reacts by analyzing html code not displayed in the Chrome browser

I am using reactJS to render an email response, for the email templates that I have dangerously tried with SetInnerHtml ReactHtmlParser, the problem is still in Chrome, Firefox always displays the HTML template complete but in Chrome the html to analyze is incomplete even when I show it in the console with console.log

Still in Chrome with dangerouslySetInnerHtml or ReactHtmlParser, the chain is incomplete, but in Firefox is complete and everything works fine.

Here is the code to display the email:

Where str is the decoded HTML code to render

This is a raw str output in Chrome which lacks all the amounts and images:

55% OFF until Monday, January 20th


UPGRADE TO START LEARNING
https://www.cybrary.it/upgrade/

-------------------------------------------------

Access all Career Path programs with
Your Cybrary Insider Pro Membership


It's easy! Pick your desired career and start learning the exact skills needed for that job.

Become A Network Engineer
https://www.cybrary.it/catalog/career-path/network-engineer/syllabus/

Become A Soc Analyst 
https://www.cybrary.it/catalog/career-path/soc-analyst-level-1/syllabus/

Become A Penetration Tester
https://www.cybrary.it/catalog/career-path/penetration-tester/syllabus/

Become A Cyber Security Engineer 
https://www.cybrary.it/catalog/career-path/cyber-security-engineer/


VIEW ALL CAREER PATHS
https://www.cybrary.it/catalog/career-paths/



© 2019 Cybrary, Inc. All Rights Reserved.
5801 University Research Court STE 150, College Park, MD 20740

If you no longer wish to receive these emails you may unsubscribe here: https://pages.cybrary.it/UnsubscribePage.html?mkt_unsubscribe=1&mkt_tok=eyJ…Z4OWF0SEFTSStLendsbHU5YlprTEF4S05qNTl6VHBzcElKZFhBU29uYWY3RGhVQkV4L1FuRiJ9.

And this is "part" of the same output from the same email but in Firefox it renders perfectly in the browser: